Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between array_merge and array + array?

People also ask

What is array_merge?

The array_merge() function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like. Note: If two or more array elements have the same key, the last one overrides the others.

What is the difference between array_merge () and Array_merge_recursive () in PHP?

The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.

How many types of arrays are there in PHP?

In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.


Here's a simple illustrative test:

$ar1 = [
   0  => '1-0',
  'a' => '1-a',
  'b' => '1-b'
];


$ar2 = [
   0  => '2-0',
   1  => '2-1',
  'b' => '2-b',
  'c' => '2-c'
];

print_r($ar1+$ar2);

print_r(array_merge($ar1,$ar2));

with the result:

Array
(
  [0] => 1-0
  [a] => 1-a
  [b] => 1-b
  [1] => 2-1
  [c] => 2-c
)
Array
(
  [0] => 1-0
  [a] => 1-a
  [b] => 2-b
  [1] => 2-0
  [2] => 2-1
  [c] => 2-c
)

Notice that duplicate non-numeric keys will take the first value using the union operator but the later one using the array_merge.

For numeric keys, the first value will be used with the union operator whereas the all the values will be used with the array_merge, just reindexed.

I generally use union operator for associative arrays and array_merge for numeric. Of course, you can just as well use the array_merge for associative, just that the later values overwrite earlier ones.


The difference is:

The + operator takes the union of the two arrays, whereas the array_merge function takes the union BUT the duplicate keys are overwritten.


array_merge() causes all numeric keys found in the input arrays to be reindexed in the resultant array. The union operator + does not cause a reindex.


array_merge vs plus

Source: https://softonsofa.com/php-array_merge-vs-array_replace-vs-plus-aka-union/

Stop using array_merge($defaults, $options):

function foo(array $options)
{
   $options += ['foo' => 'bar'];

   // ...
}

Note: array_replace function exists since PHP5.3.


The + sign only takes the value from the first occurence of an array key.
array_merge takes the value from the last occurrence of an array key.

Example:

$first = ['a'=>'one',
        'b'=>'two',
        'c'=>'three'];

$second = ['a'=>'fourth',
        'b'=>'fifth',
        'c'=>'sixth',
        '3'=>'number three'];

$merged = $first + $second;
echo "<pre> plus sign merge\n";
var_dump($merged);

$merged = array_merge($first,$second);
echo "\n array_merge function merge\n";
var_dump($merged);

This outputs:

plus sign merge
array(4) {
["a"]=>
string(3) "one"
["b"]=>
string(3) "two"
["c"]=>
string(5) "three"
[3]=>
string(12) "number three"
}

array_merge function merge
array(4) {
["a"]=>
string(6) "fourth"
["b"]=>
string(5) "fifth"
["c"]=>
string(5) "sixth"
[0]=>
string(12) "number three"
}

Interesting to note in this is that the array_merge actally erases the '3' index of number three even though it's a string, because it's a number.

So take care when merging with array_merge arrays with numerical indexes. They might lose their keys. if they are important to you precede them with a string.

so instead of '3' => 'three' use something like '_3' => 'three'


I believe array_merge overwrites duplicate non_numeric keys while $array1 + $array2 does not.