PHP arrays are a very flexible tool to store data. Sometimes it happens that you need to combine the values of two different arrays into a single array. This operation is called “merge” of the two PHP arrays and you can do it in two different ways:
- with the function array_merge
- with the + operator
But these two methods can lead to different results, depending on the following factors:
- the two arrays have one or more keys with the same value (and of the same type)
- the type of the common keys is integer or string
You should be aware of this behaviour to avoid unexpected results from your script. Let’s see the different cases.
Merge of two arrays with all keys different
If the two PHP arrays have no key in common (all keys are different) the result is as expected: an array that contains the first array plus the second one appended at the end. Moreover it makes no difference using the array_merge function or the + operator.
So, for example, if you have:
$a = [1, "hello"];
$b = [2 => 3, 3 => "world", 4 => 55];
then
print_r(array_merge($a, $b));
and
print_r($a + $b);
output the same result, that is $b appended at the end of $a:
Array
(
[0] => 1
[1] => hello
[2] => 3
[3] => world
[4] => 55
)
The reason is that the keys of $a are the integers 0, 1 and the keys of $b are the integers 2, 3, 4, so the two arrays have no key with the same value and type.
But what happens if some of the keys have the same value (and type)?
Merge of PHP arrays with some keys with identical value and type
In such a situation the result depends on the type of the keys in common.
Case 1: the array keys in common are of string type
In this case, for every couple of values with the same key, the resulting array will contain only one of the two elements. It will have the first value if you use the + operator, while the last value if you use the array_merge function.
For example with these two arrays (common key “k2”) and the + operator:
$a = ["k1" => 1, "k2" => 2];
$b = ["k2" => 3, "k3" => 4];
print_r ($a + $b);
the output is:
Array
(
[k1] => 1
[k2] => 2
[k3] => 4
)
while with the array_merge function :
print_r (array_merge($a, $b));
the output is:
Array
(
[k1] => 1
[k2] => 3
[k3] => 4
)
Case 2: the array keys in common are of integer type
In this case the + operator has the same behaviour as with string keys: for every couple of values with the same key, the resulting array will only have the first value.
Instead the array_merge function will append the second array to the first one, but with new generated keys, because a PHP array cannot have two elements with the same key. So in this case no element get lost.
In the following example the array $a has two elements with keys 0, 1, while $b has three elements whose keys are 0,1,2.
With the + operator:
$a = ["a1", "a2"];
$b = ["b1", "b2", "b3"];
print_r ($a + $b);
the output is:
Array
(
[0] => a1
[1] => a2
[2] => b3
)
while using array_merge
print_r (array_merge($a, $b));
you will get:
Array
(
[0] => a1
[1] => a2
[2] => b1
[3] => b2
[4] => b3
)
So in the merged array the keys of the array $b have been recalculated so that they are consecutive to $a keys.
Conclusion of merge with some identical keys
Let’s sum up what happens in case of one or more keys identical (value and type):
- + operator: no matter the key type, the output array contains only the element of the first array.
- array_merge function:
- integer key: the output array contains both elements, the first one with its original key, the second one with a new key
- string key: the output array contains only the element of the second array
Conclusion
To summarize, when you have to merge two PHP arrays you have two methods:
- using the + operator
- using the array_merge function
If the keys of the two arrays are all different, it does not matter which method you use: the result will always be an array that contains the second one appended at the end of the first one.
But if the two arrays have some identical key (value and type) you can have very different results. It is very important that you know how these two different methods work to avoid unexpected results.