Skip to content
Home » Difference between array_merge and array_merge_recursive in PHP

Difference between array_merge and array_merge_recursive in PHP

Summary

The difference between PHP array_merge & array_merge recursive is that array_merge overwrites similar string keys, replacing the previous value with the latest, while the array_merge_recursive assigns an array to the similar key and append the values to that array.

Introduction

We have already seen the functions array_merge and array_merge_recursive. As their names suggest, they help merge arrays in PHP. However, it is necessary to understand the core differences in these functions. This understanding will help you use the correct function in the correct place.

In this article, we will explore the difference between array_merge and array_merge_recursive in PHP. Before that, we will recommend you to get yourself familiarised with these functions. Here are the links to the articles we have done for you.

PHP array_merge : How to use with examples in PHP

PHP array_merge_recursive: How to use with examples in PHP

Once you’re done, we are good to go.

What is the difference between array_merge and array_merge_recursive?

As we have mentioned already, these functions merge PHP arrays. Then what’s the difference? The difference lies in their dealing with similar string keys. PHP array_merge and array_merge_recursive distinctively behave when they encounter similar keys that are strings. 

Let’s understand the array_merge and array_merge_recursive difference in PHP using comparative examples. 

PHP array_merge overwrites similar string keys, replacing the previous value with the latest, while the array_merge_recursive assigns an array to the similar key and append the values to that array. The following example will clarify the difference.

Here’s the example and output for array_merge when similar keys are strings.

<?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  
)
*/
?>

See! It overwrites the “Emp#2” key that is similar in both arrays. The previously occurring value “Richard” is replaced with the latest value “Narnie”. 

Let’s check out how the array_merge_recursive deals with it.

<?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_recursive($array1, $array2);
 
print_r($merged_array);
 
/*
OUTPUT
Array
(
    [Emp#1] => Franklin
    [Emp#2] => Array
        (
            [0] => Richard
            [1] => Narnie
        )
 
    [Emp#3] => Jake
    [Emp#4] => Tyson
    [Emp#5] => Kylie
)
*/
?>

Due to its recursive nature, PHP array_merge_recursive retains the values by appending them to an array with the key “Emp#2”.

When to use array_merge and array_merge_recursive?

Both these functions have their use cases. It makes no sense to use a function like array_merge that could potentially lose data to overwriting, but the fact is that it has a crucial use case. The most favourable scenario for using array_merge is merging configuration arrays. Let’s have a look at that.

Configuration settings – array_merge use case

Configuration files are an essential aspect of development. Developers usually tinker around the config files. Immatures find it daunting because they probably mess their projects up when unknowingly modifying things they shouldn’t. 
But let’s imagine a scenario with two configuration arrays, one with the old configuration settings and another with the latest ones. It is self-evident that the latest need to override the previous settings. So, we realise that we certainly would need a function like the array_merge to overwrite the values of similar keys. Here’s how.

<?php
$old_config = [
    "src" => '/project-src',
    "dest" => '/project-dest',
    "script" => 'run --start',
    "version" => '1.0',
    "author" => 'Alex'
];
 
$new_config = [
    "src" => '/project-src',
    "dest" => '/project-dest-new',
    "script" => 'run',
    "version" => '1.1',
    "author" => 'Alex'
];
 
$new_config = array_merge($old_config, $new_config);
 
print_r($new_config);
 
/*
OUTPUT
Array
(
    [src] => /project-src
    [dest] => /project-dest-new
    [script] => run
    [version] => 1.1
    [author] => Alex
)
*/

Voila! It doesn’t intermingle the config setting but overrides the old settings. That’s what we want. 

So, that’s a practical example for using array_merge. Next, let’s see an example where array_merge_recursive fits well.

User data – array_merge use case

Here is an array of user data accumulated in different arrays. Notice that some users data has been present in multiple arrays.

$array1 = [
    "User#1" => ["Status"=>"Active","Roles"=>["Monitor","Collaborator"],"Hi! I am junior developer"],
    "User#2" => ["Status"=>"Offline","Roles"=>["Developer","QA"],"Hi! I am QA engineer"],
    "User#3" => ["Status"=>"Left","Roles"=>["Trainee","UI/UX"],"Hi! I am UI/UX deisgner"],
];
$array2 = [
    "User#1" => ["Roles"=>"Developer","I develop the backend system"],
    "User#2" => ["I design the UI/UX of the application"],
];
$array3 = [
    "User#3" => ["Roles"=>["Trainee","UI/UX"],"MovedTo"=>"Project Zero"],
    "User#4" => ["Status"=>"Active","Roles"=>["Project Manager"]],
];

There should be a function to organize the data in one array for easy access. That’s where the array_merge_recursive function could be helpful Here’s the output with the array_merge_recursive.

/*
OUTPUT
Array
(
    [User#1] => Array
        (
            [Status] => Active
            [Roles] => Array
                (
                    [0] => Monitor
                    [1] => Collaborator
                    [2] => Developer
                )
 
            [0] => Hi! I am junior developer
            [1] => I develop the backend system
        )
 
    [User#2] => Array
        (
            [Status] => Offline
            [Roles] => Array
                (
                    [0] => Developer
                    [1] => QA
                )
 
            [0] => Hi! I am QA engineer
            [1] => I design the UI/UX of the application
        )
 
    [User#3] => Array
        (
            [Status] => Left
            [Roles] => Array
                (
                    [0] => Trainee
                    [1] => UI/UX
                    [2] => Trainee
                    [3] => UI/UX
                )
 
            [0] => Hi! I am UI/UX deisgner
            [MovedTo] => Project Zero
        )
 
    [User#4] => Array
        (
            [Status] => Active
            [Roles] => Array
                (
                    [0] => Project Manager
                )
 
        )
 
)
*/

Super! We have seen some pretty good use cases to clarify array_merge and array_merge_recursive difference in PHP.

Conclusion

In this article, we have seen array_merge and array_merge_recursive difference in PHP with examples. The difference is that the array_merge overwrites similar string keys’ values, replacing the previous value with the latest. In comparison to this, the array_merge_recursive function appends the values to an array and assign them to the respective key.

We have also seen some scenarios or use cases for both these functions. Hopefully, you are now clear about the difference between these functions. That’s all for this article. Stay tuned for more interesting PHP articles and tutorials.

Want to explore more useful PHP tutorials?

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

How to merge 2 arrays with the same keys in PHP

array_merge_recursive: How to sue with examples in PHP

PHP array_filter: How to use with examples in PHP