PHP Array Merge: 3 Methods (loop, array_merge and +)

H Bahonar
4 min readDec 17, 2022

PHP array merge, concat, or append 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 to concat 2 arrays

PHP array_merge() is a built-in function to append 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 merges 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 append array

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).

Merge two arrays in PHP with loops

The next method to merge or union two arrays is by using PHP loops. This is not recommended method but maybe you will need this in your project.

In this way, the second array will be appended to the end of the first array.

$cars1 = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');
$cars2 = array('vol' => 'Volvo', 'toy' => 'Toyota');
foreach($cars2 as $key => $car){
$cars1[$key]=$car;
}
var_dump($cars1);

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)

In this example, we iterate the second array and push its value to the first array. Pushing value to an array can be done with the array_push() function too.

If there is a conflict in the keys, the value of the first array will be changed by the value of the second array.

$cars1 = array(1 => 'Ferrari', 2 => 'Benz', 3 => 'BMW');
$cars2 = array(3 => 'Volvo', 4 => 'Toyota');
foreach($cars2 as $key => $car){
$cars1[$key]=$car;
}
var_dump($cars1);

Output

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

Read more: https://honarsystems.com/php-array-merge/

--

--

H Bahonar

I am a Web Developer & Designer. Specialist in WordPress. CEO of Honar Systems