PHP array merge, concat, union, 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. Array merging stands out as a fundamental operation for combining arrays and their elements into a single cohesive unit.
In This tutorial, we are going to discuss how to merge two arrays in PHP with the array_merge() function, +
operator, and loop to explore its various use cases, syntax, and practical examples to help you harness its full potential. So, let’s dive in and explore the art of merging arrays in PHP!
Read more about the PHP array in our tutorial.
Array merge content in PHP
PHP array_merge() function to concat 2 arrays
PHP array merge refers to the process of combining and union multiple arrays into a single array, preserving the keys and values of the original arrays. The resulting merged array contains all the elements from the input arrays, maintaining their original order.
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, ...);
The array_merge() function accepts one or more arrays as parameters and returns a new array containing the merged elements.
array1 | Required | The first array to merge. This parameter is optional since PHP 7.4.0. |
array2 | Optional | The second array merges with the first one. |
Returned Value | Returns 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.
$array1 = array('Ferrari', 'Benz');
$array2 = array('Volvo', 'Toyota');
$mergedArray = array_merge($array1, $array2);
print_r($mergedArray);
Output
Array
(
[0] => Ferrari
[1] => Benz
[2] => Volvo
[3] => Toyota
)
In the above example, we merge two arrays, $array1
and $array2
, using array_merge()
. The resulting array, $mergedArray
, contains all the elements from both arrays.
Merge an associative array in PHP
$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 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 a 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 also be done with the array_push() function.
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)
Merging Nested Arrays
While the array_merge()
function can handle basic array merging, but it falls short when dealing with merging nested arrays. Thankfully, PHP provides the array_merge_recursive()
function, which enables us to merge a nested array in PHP, including their nested elements, in a recursive manner.
$array1 = array('fruits' => array('apple', 'banana'));
$array2 = array('fruits' => array('orange', 'grape'));
$mergedArray = array_merge_recursive($array1, $array2);
print_r($mergedArray);
Output
Array
(
[fruits] => Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
)
)
In this example, we utilize array_merge_recursive()
to merge two arrays with nested arrays. The resulting merged array $mergedArray
combines the nested arrays while preserving the structure.