php array merge array_merge and +

PHP Array Merge: 2 Methods (array_merge and +)

PHP array merge is an action to merge the elements of one or more arrays together into one array and you can do it in several ways. In This tutorial, we are going to discuss how to merge two arrays in PHP with the array_merge() function and + operator.

Read more about the PHP array in our tutorial.

PHP array_merge() function

PHP array_merge() is a built-in function to merge the elements of one or more arrays together so that the values of one are appended to the end of the first one. It returns the resulting array. This function is available in PHP version 4 and higher.

array_merge(array1, array2, ...);
array1RequiredThe first array to merge. This parameter is optional since PHP 7.4.0.
array2OptionalThe second array to merge with the first one.
Returned ValueReturns the resulting array.

PHP array_merge() function since PHP 7.4.0 can be called without any parameter. If you call this function without any parameter it will return null.

$cars1 = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');
$cars2 = array('vol' => 'Volvo', 'toy' => 'Toyota');

var_dump(array_merge($cars1, $cars2));

Output:

array (size=5)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)
  'vol' => string 'Volvo' (length=5)
  'toy' => string 'Toyota' (length=6)

If the input arrays have the same string keys, then the last value for that key will overwrite the previous ones.

$cars1 = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');
$cars2 = array('vol' => 'Volvo', 'toy' => 'Toyota', 'fer' => 'Ferrari 2');

var_dump(array_merge($cars1, $cars2));

Output

array (size=5)
  'fer' => string 'Ferrari 2' (length=9)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)
  'vol' => string 'Volvo' (length=5)
  'toy' => string 'Toyota' (length=6)

In this example “Ferrari 2” takes the “Ferrari”‘s place because the keys are the same.

Values in the input arrays with numeric keys will be renumbered with incrementing keys starting from zero.

$cars1 = array(1 => 'Ferrari', 2 => 'Benz', 3 => 'BMW');
$cars2 = array(1 => 'Volvo', 2 => 'Toyota');

var_dump(array_merge($cars1, $cars2));

Output

array (size=5)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)
  3 => string 'Volvo' (length=5)
  4 => string 'Toyota' (length=6)

If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new ordered index array with integer keys starting at 0.

$cars = array(1 => 'Ferrari', 2 => 'Benz', 3 => 'BMW');

var_dump(array_merge($cars));

Output

array (size=3)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)

Note: If you want to reorder the numeric keys, use the array_merge() function.

PHP + array union operator to merge arrays

If you want to append array elements from the second array to the first array while not re-indexing, use the + array union operator:

$cars1 = array(1 => 'Ferrari', 2 => 'Benz', 3 => 'BMW');
$cars2 = array(1 => 'Volvo', 2 => 'Toyota');

var_dump($cars1 + $cars2);

Output

array (size=3)
  1 => string 'Ferrari' (length=7)
  2 => string 'Benz' (length=4)
  3 => string 'BMW' (length=3)

The keys from the arrays will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the conflicted key’s element from the second array will be ignored.

PHP array_merge vs + union operator

When you use them with associative arrays, there is no difference between array_merge() and + operand. Only in the array_merge() function if your arrays are indexed, the keys won’t be reindexed.

$cars1 = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');
$cars2 = array('vol' => 'Volvo', 'toy' => 'Toyota');

var_dump(array_merge($cars1, $cars2));
var_dump($cars1 + $cars2);

Output

array (size=5)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)
  'vol' => string 'Volvo' (length=5)
  'toy' => string 'Toyota' (length=6)

array (size=5)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)
  'vol' => string 'Volvo' (length=5)
  'toy' => string 'Toyota' (length=6)

The difference is when you are going to use two arrays that there is conflict in their keys.

$cars1 = array(1 => 'Ferrari', 2 => 'Benz', 3 => 'BMW');
$cars2 = array(3 => 'Volvo', 4 => 'Toyota');

var_dump(array_merge($cars1, $cars2));
var_dump($cars1 + $cars2);

Output

array (size=5)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)
  3 => string 'Volvo' (length=5)
  4 => string 'Toyota' (length=6)

array (size=4)
  1 => string 'Ferrari' (length=7)
  2 => string 'Benz' (length=4)
  3 => string 'BMW' (length=3)
  4 => string 'Toyota' (length=6)

In this example, there are two differences. The first one is ordering the keys, in PHP array_merge() function keys are ordered from 0, and in + operand orders are not changed.

The second difference between array_merge() and + is the values. In + operand, the 3 key is in both arrays and the operand ignored the conflicted key and value in the second array (+ operand deleted 3 key and its value from results).

Shopping Cart