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
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
)
