PHP Array_Merge

I have been doing a lot of work with arrays lately, and was recently working with a couple of arrays that needed to be merged together into one array. One way to do this is with a foreach loop. Something like this:

$arrayOne = array('cat', 'dog', 'turkey');
$arrayTwo = array('peach', 'apple', 'banana');
foreach ($arrayTwo as $aRow) {
    $arrayOne[] = $aRow;

And we get this as a result:


Simple, but why do that when you can do this in one line using array_merge:

$newarray = array_merge($arrayOne, $arrayTwo);

and get this:


As you can see with one line I've merged the arrays. There are some gotchas so, let's go a little deeper into array_merge

Before I go further, I want to put a plug in for my free PHP variable checker "newchk." All the array displays you see in this post are using newchk to put the array output to the screen. I find the utility useful in my development. Newchk is is available here. On the next example, I included the call to newchk so could see how to use it to display the array.

Array merge works with two or more arrays. Let's do it:

$arrayOne = array('cat', 'dog', 'turkey');
$arrayTwo = array('peach', 'apple', 'banana');
$arrayThree = array('dodge', 'ford', 'mazda');
$threeArrays = array_merge($arrayOne, $arrayTwo, $arrayThree);
new chk ($threeArrays);

And we get this:


As you can see newchk is easy to use, and you get great results. Let's get back to array_merge.


What if one of the arrays was a null?

$arrayOne = array('cat', 'dog', 'turkey');
$arrayTwo = array();
$arrayThree = array('dodge', 'ford', 'mazda');
$arrayNull = array_merge($arrayOne, $arrayTwo, $arrayThree);

Which results in:


So null arrays are just ignored.


Now let's look at associative arrays.

$arrayOne = array( 'animal1' =>'cat', 'animal2' =>'dog', 'animal3' =>'turkey');
$arrayTwo = array('fruit1' => 'peach', 'fruit2' => 'apple', 'fruit3' => 'banana');
$newarray = array_merge($arrayOne, $arrayTwo);

What we expect:


But what if we had the same key in each of arrays?

$arrayOne = array( 'animal1' =>'cat', 'sameKey' =>'dog', 'animal3' =>'turkey');
$arrayTwo = array('fruit1' => 'peach', 'sameKey' => 'apple', 'fruit3' => 'banana');
$arrayThree = array( 'car1' => 'dodge', 'sameKey' => 'ford', 'car3' => 'mazda');
$newArr = array_merge($arrayOne, $arrayTwo, $arrayThree);

What I've done is put the same associative key in each array, and here are the results;


Whoops, what just happened. Each of the three arrays have the same key. The first array picked up the associative key, but in each array merged after the first array, the arrayOne key was used, but the value was set to the next array. In the end, we have the third array value as part of the first array. That didn't happen when we didn't use associative keys.


What if we used intergers for keys, would we get the same results?

$arrayOne = array( 0 =>'cat', 2 =>'dog', 4 =>'turkey');
$arrayTwo = array( 0 => 'peach', 2  => 'apple', 4 => 'banana');
$newArr = array_merge($arrayOne, $arrayTwo);


As you can see the keys are renumbered. So if the associative array has string keys, be careful that the keys are not the same in each array when using array merge. Numeric keys are not overwritten.


Suppose you want to merge the array, but not if the numeric keys are the same, and if they are the same, you want to maintain the first value. Well, will do something different, called array union, which is just a plus sign, let's do it.

$arrayOne = array( 0 =>'cat', 2 =>'dog', 4 =>'turkey');
$arrayTwo = array( 0 => 'peach', 2  => 'apple', 4 => 'banana');
$unionArr =  $arrayOne +  $arrayTwo;

And we get:


Notice that the keys that are the same are ignored in the second array.


Finally when using array_merge consider always casting the two arrays as arrays. Here's why. First, let merge a string and an array.

$arrayOne = 'The quick fox blew it by not turning this into an array";
$arrayTwo = array( 0 => 'peach', 2  => 'apple', 4 => 'banana');
$stringArr =  array_merge( $arrayOne , $arrayTwo);
new chk( $arrayOne, $arrayTwo , $stringMerge );


Array_merge can not handle the string, and we end up with a null array, but let's run the exact same parameters, except we'll cast the string as an array.

$arrayOne =  (array) 'The quick fox blew it by not turning this into an array";
$arrayTwo = array( 0 => 'peach', 2  => 'apple', 4 => 'banana');
$stringMerge =  array_merge( $arrayOne , $arrayTwo);
new chk( $arrayOne, $arrayTwo , $stringMerge );

And we get:


Now that we cast the string as an array, array_merge works as expected.


It's always a good idea when using array_merge to cast each array as an array, just to avoid a string nullifing the merge. If you want to get rid of any duplicate values from an array after the merge, I recommend using array_unique, and if you want to clear out any nulls try array_filter. Here's the final example of these two commands in action.

$arrayOne = array( 'animal1' =>'cat', 'animal2' =>'dog', 'animal3' =>'turkey');
$arrayTwo = array('fruit1' => '', 'sameKey' => '', 'fruit3' => 'banana');
$arrayThree = array( 'animal4' => 'cat', 'animal5' => 'lion', 'animal6' => 'turkey');


As you can see, we have duplicate values and nulls in the result array after the array_merge. Let's clean out the duplicate values with array_unique, and then clean out the nulls with array_filter. To the above code we'll add:

$uniqueArr = array_unique($mergeArr);
$filterArr = array_filter($uniqueArr);

And here are the last two arrays:


Notice how the array_unique took out the dups, and array_filter took out the nulls. It's always a good idea to clean your array before using it later on in your code. Happy coding, until next time.

Comments are closed.