How to preserve array keys for an associative array in php

Insert an associtive array ain another assoInsert an associative array in another associative array

array_splice() or array_merge() php functions doesn’t preserve keys. So if we use these functions then new array key will be [0]
Instead we will use array_splice() and use ‘+’ to concatenate the arrays

$array1 = array('a'=>'apple', 'b'=>'banana', '42'=>'pear', 'd'=>'orange');
$array2 = array('10' => 'grapes', 'z' => 'mangoes');
$index = 4; //index of array1 where you would like to insert array2
$finalArray = array_slice($array1, 0, $index, true) +
                $array2  +
			    array_slice($array1, $index, NULL, true);

print_r($finalArray);

/* Result will be:
Array
(
    [a] => apple
    [b] => banana
    [42] => pear
    [d] => orange
    [10] => grapes
    [z] => mangoes
)
*/