How to merge two or more arrays in PHP?

In this tutorial, you will learn how to merge two or more arrays in PHP.

PHP has three in-built functions to merge arrays –

1. PHP array_merge() function

The PHP array_merge() function is used to merge the values of one or more arrays.

array_merge(array1, array2, array3, ...)
  • This array_merge function can take multiple arrays as arguments, but at least one array is required.
  • This function returns the merged array, and the merged array does not preserve the same numeric keys, as specified in the original array. (In the merged array numeric keys start at 0 and increases by 1 for each value).
  • If two or more arrays have the same user-defined keys, the key of the after-specified array will replace the other keys.
<?php
$array1 = array(25, 'PHP', 41.23, 'Java', 'Python');
$array2 = array('C++', 'JavaScript');

$mergedArray = array_merge($array1, $array2);

print_r($mergedArray);
Array
(
    [0] => 25        
    [1] => PHP       
    [2] => 41.23     
    [3] => Java      
    [4] => Python    
    [5] => C++       
    [6] => JavaScript
)

The array_merge function overrides the same user-defined keys.

<?php
$array1 = array("name" => "John Doe", "age" => 24);
$array2 = array("name" => "Jane", "age" => 21);
$array3 = array("name" => "Mark");

$mergedArray = array_merge($array1, $array2, $array3);

print_r($mergedArray);
Array
(
    [name] => Mark
    [age] => 21   
)

The merged array does not preserve the same numeric keys

<?php
$array1 = array(5 => "Mark", 7 => "John");

$mergedArray = array_merge($array1);

print_r($mergedArray);
Array
(
    [0] => Mark
    [1] => John
)

2. PHP array_merge_recursive() function

The array_merge_recursive function is almost similar to the array_merge function.

The difference between these functions is that the array_merge function will override all the same user-defined keys into the same user-defined key of the after-specified array.

But the array_merge_recursive() function preserves all values of the same keys.

<?php
$array1 = array("name" => "John");
$array2 = array("name" => "Mark");

$arrayMerge = array_merge($array1, $array2);
$arrayMergeRcu = array_merge_recursive($array1, $array2);

echo "#array_merge\n";
print_r($arrayMerge);

echo "\n#array_merge_recursive\n";
print_r($arrayMergeRcu);
#array_merge
Array
(
    [name] => Mark     
)

#array_merge_recursive  
Array
(
    [name] => Array    
        (
            [0] => John
            [1] => Mark
        )

)

3. PHP array_combine() function

With the help of the array_combine() function, you can merge two arrays as key-value pairs.

array_combine(keys, values)
  • This function takes two arrays as arguments, one for the keys and the other for the values.
  • Both arrays must have equal number of elements.
  • It returns FALSE if the number of elements of both arrays does not match, else it will return the combined array.
<?php
$array1 = array("name", "age");
$array2 = array("John", 24);

$combinedArray = array_combine($array1, $array2);
print_r($combinedArray);
Array
(
    [name] => John
    [age] => 24   
)

Leave a Reply

Your email address will not be published. Required fields are marked *