While this question is quite old I just want to add another possibility of doing a merge while keeping keys.
Besides adding key/values to existing arrays using the +
sign you could do an array_replace
.
$a = array('foo' => 'bar', 'some' => 'string');
$b = array(42 => 'answer to the life and everything', 1337 => 'leet');
$merged = array_replace($a, $b);
The result will be:
Array
(
[foo] => bar
[some] => string
[42] => answer to the life and everything
[1337] => leet
)
Same keys will be overwritten by the latter array.
There is also an array_replace_recursive
, which do this for subarrays, too.