Skip to content
Home » Functions » PHP array_merge: How to use with examples in PHP

PHP array_merge: How to use with examples in PHP

What is PHP array_merge?

Description

PHP array_merge function merges one or more arrays.

Function Signature

array_merge(array ...$arrays): array

Arguments

  • $arrays – The arrays to merge.

Return

This function returns the resulting array after merging. It returns an empty array if no arguments are passed. 

Notes

  • If the array keys are strings, then array_merge overwrites similar keys with the latest value.
  • The arrays with numeric keys are reindexed after the merge.

Introduction – PHP array_merge

The array_merge function conveniently merges a variable number of arrays using just a function call. Imagine using a loop over ten different arrays and catering overwrites yourself – daunting, isn’t it? Fortunately, PHP provides array_merge to fuse the PHP arrays together.

PHP array_merge

In this article, we will explore the array_merge function through examples and see some important notes along the way. Let’s get to the job!

Usage Example#1 – PHP array_merge

Here’s an example of array_merge with associative arrays having numeric keys.

<?php

$array1 = [1,2,3,4,5];
$array2 = [6,7,8,9,10];
$array3 = [11,12,13,14,15];
$array4 = [16,17,18,19,20];
 
$merged_array = array_merge($array1, $array2, $array3, $array4);
 
print_r($merged_array);
 
/*
OUTPUT
Array      
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [
*/
?>

See! It reindexes the resultant array. Next, let’s try the same experiment with PHP associative arrays having string keys.

Usage Example#2 – PHP array_merge

<?php

$array1 = ["Emp#1"=>"Franklin","Emp#2"=>"Richard","Emp#3"=>"Jake"];
$array2 = ["Emp#4"=>"Tyson","Emp#2"=>"Narnie","Emp#5"=>"Kylie"];
 
$merged_array = array_merge($array1, $array2);
 
print_r($merged_array);
 
/*
OUTPUT
Array
(
    [Emp#1] => Franklin
    [Emp#2] => Narnie  
    [Emp#3] => Jake    
    [Emp#4] => Tyson  
    [Emp#5] => Kylie  
)
*/
?>

Have you checked out what has happened to a similar key, “Emp#2”? It has been overwritten “Richard” with later occurring value “Narnie”. This difference is essential because numeric keys are reindexed, as we’ve seen already.

Next, let’s see complicated array structures and establish that the function doesn’t pay much attention to array values; rather, it looks out for the keys.

Usage Example#3 – PHP array_merge

Here are relatively complex arrays.

<?php

$students_electives_1 = [
    "Andy" => ["ID"=>"1001", "Elective"=>"Computer Science"],
    "Benjamin" => ["ID"=>"1002", "Elective"=>"Electronics"],
    "Catheline" => ["ID"=>"1003", "Elective"=>"Economics"],
    "Dexter" => ["ID"=>"1004", "Elective"=>"Computer Science"],
    "Eion" => ["ID"=>"1004", "Elective"=>"Computer Science"],
    "Franklin" => ["ID"=>"1005", "Elective"=>"Mathematics"],
];
 
$students_electives_2 = [
    "Gina" => ["ID"=>"1006", "Elective"=>"Chemistry"],
    "Hudson" => ["ID"=>"1007", "Elective"=>"Electronics"],
    "Ira" => ["ID"=>"1008","Elective"=>"Mathematics"],
    "Jessica" => ["ID"=>"1009","Elective"=>"Economics"],
    "Kelvin" => ["ID"=>"1010","Elective"=>"Computer Science"],
    "Liam" =>  ["ID"=>"1011","Elective"=>"Mathematics"],
];
 
$merged_array = array_merge($students_electives_1, $students_electives_2);
 
print_r($merged_array);
 
/*
OUTPUT
Array
(
    [Andy] => Array
        (
            [ID] => 1001
            [Elective] => Computer Science
        )
 
    [Benjamin] => Array
        (
            [ID] => 1002
            [Elective] => Electronics    
        )
 
    [Catheline] => Array
        (
            [ID] => 1003
            [Elective] => Economics
        )
 
    [Dexter] => Array
        (
            [ID] => 1004
            [Elective] => Computer Science
        )
 
    [Eion] => Array
        (
            [ID] => 1004
            [Elective] => Computer Science
        )
 
    [Franklin] => Array
        (
            [ID] => 1005
            [Elective] => Mathematics
        )
 
    [Gina] => Array
        (
            [ID] => 1006
            [Elective] => Chemistry
        )
 
    [Hudson] => Array
        (
            [ID] => 1007
            [Elective] => Electronics
        )
 
    [Ira] => Array
        (
            [ID] => 1008
            [Elective] => Mathematics
        )
 
    [Jessica] => Array
        (
            [ID] => 1009
            [Elective] => Economics
        )
 
    [Kelvin] => Array
        (
            [ID] => 1010
            [Elective] => Computer Science
        )
 
    [Liam] => Array
        (
            [ID] => 1011
            [Elective] => Mathematics
        )
 
)
*/
?>

So, the function can merge PHP arrays irrespective of their dimensions and structures. That’s because the function doesn’t really care about the values. That being said, let’s rewind to what we saw in the first usage example.

So, we saw that the function reindexes the numeric keys after merging the arrays. This behavior is useful and most desirable to retain all the elements. However, if we don’t want to reindex, then array_merge PHP doesn’t offer any alternatives. Gladly, we have a workaround for this, and that is what we will see next.

The Union ‘+’ Operator

The union operator ‘+’ can be used to merge arrays without reindexing the numeric keys. Let’s redo example#1 and observe the output before reaching any conclusion.

<?php

$array1 = [1,2,3,4,5];
$array2 = [6,7,8,9,10];
$array3 = [11,12,13,14,15];
$array4 = [16,17,18,19,20];
 
$merged_array = $array1 + $array2 + $array3 + $array4;
 
print_r($merged_array);
 
/*
OUTPUT
Array      
 
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
*/
?>

So it retains the values of the first array and ignores other values. That means it ignores the matching keys from other arrays. 

Updates – PHP 7.4 and Above

PHP supports spread operator in 7.4 and above versions. A spread operator expands an iterable into individual elements. It is ideal for merging arrays and is known to be faster than the array_merge in PHP.

This image has an empty alt attribute; its file name is spread-1024x685.png

Let’s go through an example using the spread operator.

<?php
$array1 = [1,2,3,4,5];
$array2 = [6,7,8,9,10];

$merged_array = [...$array1, ...$array2];

print_r($merged_array);

/*
OUTPUT
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
*/
?>


Voila! It works like a charm!!

Conclusion

This article explores array_merge in PHP through examples and discusses some important caveats related to how it deals with similar keys. Beyond that, it also touches upon the use of the union and spread operators in the context of merging PHP arrays. We hope you’ve better insights about the function. That’s it for this article. Stay tuned for more!

Want to explore more useful PHP tutorials?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

Find a value in a multidimensional PHP array.

Search for multiple values in PHP array

How to filter array of objects by value in PHP.