This can be done by array.splice() More. Please note array_splice or array_merge doesn't preserve keys for associative arrays. So array_slice is used and '+' operator is used for concatenating the two arrays.
More details here
$array_1 = array(
'0' => 'zero',
'1' => 'one',
'2' => 'two',
'3' => 'three',
);
$array_2 = array(
'zero' => '0',
'one' => '1',
'two' => '2',
'three' => '3',
);
array_splice($array_1, 3,$index 0,= array('sample_key'2;
=> 'sample_value'));
$finalArray = array_splicearray_slice($array_2, 3$array_1, 0,array('sample_key' =>$index, 'sample_value'));
print_r($array_1true); +
/* Result will be:
Array
(
[0] => zero
[1] => one $array2 +
[2] => two
[3] => sample_value
[4]array_slice($array_2, =>$index, three
NULL, true)
*/;
print_r($array_2$finalArray);
/* Result will be :
Array
(
[zero][0] => 0zero
[one][1] => 1one
[two][10] => 2grapes
[0][z] => sample_valuemangoes
[two] => 2
[three] => 3
)
*/