The beauty of PHP is that you can do one thing in several different ways. And, yes, this is the case with PHP arrays too – they can be merged using the + array union operator or you could use the array_merge() function. However, there is a difference between array_merge and array + array, so let’s take a look at the functions we’re are dealing with.
What is the array_merge() function?
The array_merge() function is an inbuilt function that merges the elements of arrays together – it can merge only one array or it can merge more. The function merges arrays regardless of the array type – indexed or numerical, associative or multidimensional – into one array. Array_merge() function merges the elements of one or more arrays together so that the values of one array are appended to the end of the previous array. Array_merge() function returns the resulting array:
array_merge ( array ...$arrays ) : array
There are a few things you should keep in mind when using array_merge() function:
- If two or more array elements have the same string keys, then the later value for that string key will overwrite the values placed before the last value.
- However, if the arrays contain numeric keys, the later value will not override the original value, but will be appended.
- In cases that have values in the input arrays with numeric keys, the values will be renumbered with incrementing keys, obviously starting from zero in the resulting array.
If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator. The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key’s element from the second array will be ignored.
What is the + array union operator?
Array union operator (+) is a binary operator, which means it merges two arrays at a time and produces the resulting array. The union operator appends the array on the right-hand side at the end of the left-hand array.
$resulting_array = $array1 + $array2;
Again, there is an important issue to keep in mind in the case of the + array union operator:
The + array union operator loses key matching – all keys of the second array are ignored if their equivalent key already exists in the first array. In this case, the rest of the keys and their comparable values of the second array are appended to the first. When dealing with a numeric array the index of the array on the right-hand side which is the same as a left-hand array will therefore be ignored in the resulting array.
Let’s write a bit of PHP code to demonstrate the difference between array_merge() and + array union operator.
<?php $array1 = array("fruit" => "apple", 2, 4); $array2 = array("a", "b", "fruit" => "banana", "vegetable" => "carrot", 4); $resulting_array = array_merge($array1, $array2); print_r($resulting_array); /* The output will be like this Array ( [fruit] => apple  => 2  => 4  => a  => b [vegetable] => carrot  => 4 ) Elements with the same key from the second array are overwriting elements from the first array. Also, elements are re-indexing. */ $resulting_array = $array1 + $array2; print_r($resulting_array); /* Now the output is a bit different Array ( [fruit] => apple  => 2  => 4 [vegetable] => carrot  => 4 ) */ ?>