Combining two or more arrays whilst keeping their keys intact in PHP is deceptively simple – though not if you assume you need to use a function like array_merge to achieve this!

As you scroll through the various array functions available to you, you will notice that quite a few seem to drop the array keys after they are done, which is pretty useless if all you want to do is basically add two arrays to one another to form one big keyed array.

So how do you do this then?

Simple really. Just literally add them together!

$resultarr = $array1 + $array2;

And just in case you are wondering what the hell is going on, remember that when applied to arrays, the + acts as a union operator.

Nice!