Let’s imagine that we have two arrays:
$array_1 = array(
'0' => 'zero',
'1' => 'one',
'2' => 'two',
'3' => 'three',
);
$array_2 = array(
'zero' => '0',
'one' => '1',
'two' => '2',
'three' => '3',
);
Now, I’d like to insert array('sample_key' => 'sample_value') after third element of each array. How can I do it?
array_slice()can be used to extract parts of the array, and the union array operator (+) can recombine the parts.This example:
gives:
Array ( [zero] => 0 [one] => 1 [two] => 2 [my_key] => my_value [three] => 3 )