There is no problem not worth solving, and no technology not worth learning!

In PHP 7.4, add elements to a specific position in the index array (array_splice)

打印运行结果,符合预期

1. Add elements to a specific position in the index array, and now plan to add 2 elements, after price.


$blockOrder = [
	"title",
	"subtitle",
	"price",
	"on-site-message",
	"variants",
	"count",
	"merit-0",
	"merit-1",
	"merit-2",
	"merit-3",
	"payment"
];



2. Based on array_splice — remove a part of the array and replace it with other values, the code is implemented as follows,


$blockOrder = [
	"title",
	"subtitle",
	"price",
	"on-site-message",
	"variants",
	"count",
	"merit-0",
	"merit-1",
	"merit-2",
	"merit-3",
	"payment"
];

print_r($blockOrder);

$blockOrder2 = [
	"automatic-discount-tag",
	"automatic-discount-bxgety"
];

array_splice($blockOrder, 3, 0, $blockOrder2);

print_r($blockOrder);


3. Print the running results, in line with expectations. as shown in Figure 1

打印运行结果,符合预期
Figure 1


Array
(
    [0] => title
    [1] => subtitle
    [2] => price
    [3] => on-site-message
    [4] => variants
    [5] => count
    [6] => merit-0
    [7] => merit-1
    [8] => merit-2
    [9] => merit-3
    [10] => payment
)
Array
(
    [0] => title
    [1] => subtitle
    [2] => price
    [3] => automatic-discount-tag
    [4] => automatic-discount-bxgety
    [5] => on-site-message
    [6] => variants
    [7] => count
    [8] => merit-0
    [9] => merit-1
    [10] => merit-2
    [11] => merit-3
    [12] => payment
)




 

 

Need long-term technical maintenance or remote troubleshooting?

I am a PHP / Go backend engineer with 15+ years of experience, focused on existing system maintenance, bug fixing, performance optimization, server troubleshooting, WordPress maintenance, and small feature iterations.

If your project is facing any of the following issues, we can start with a small troubleshooting task first:

  • ✅ PHP / Laravel / Yii2 legacy systems without active maintenance
  • ✅ Go / Gin backend APIs that need troubleshooting or optimization
  • ✅ Slow, broken, or unstable WordPress websites
  • ✅ Nginx / MySQL / Redis / Linux server issues
  • ✅ CDN / Cloudflare / DNS / HTTPS configuration problems
  • ✅ Long-term remote technical support or part-time maintenance

More details: About Me & Collaboration

WeChat: 13980074657
Email: shuijingwanwq@gmail.com
Telegram: @shuijingwan
GitHub: https://github.com/shuijingwan

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.