What is PHP array_merge_recursive?
PHP array_merge_recursive merges one or more arrays recursively.
array_merge_recursive(array ...$arrays): array
$arrays– One or more arrays to be merged
This function merges the $arrays passed in the argument and returns the merged array. If there is no argument, it returns an empty array.
Introduction – PHP array_merge_recursive
PHP array_merge_recursive is a robust function capable of merging arrays recursively. The recursive merge creates an array for similar keys rather than overwriting, a feature that sets it apart for array_merge and makes it favorable than it.
The array_merge_recursive doesn’t overwrite values of similar keys. Instead, it creates an array and appends the values to that array. That’s why you do not lose any data to overwriting. We will review an example from the array_merge article and see the differences in the output. So, in this article, we will explore array_merge_recursive in PHP. Stay with us till the end and learn a new function in PHP.
Usage Example#1 – PHP array_merge_recursive
Here, we will review an example from array_merge to show you the difference between the two functions and emphasize the vital part of array_merge_recursive.
<?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); ?>
We had seen this array before and merged it using array_merge, and it produced output as.
/* OUTPUT Array ( [Emp#1] => Franklin [Emp#2] => Narnie [Emp#3] => Jake [Emp#4] => Tyson [Emp#5] => Kylie ) */
It overwrites “Richard” with a later occurring value “Narnie” just because they have a similar key “Emp#2”. We lose essential data to the overwriting protocol of the function. However, PHP array_merge_recursive is robust in this regard and produces the output as given below.
/* OUTPUT Array ( [Emp#1] => Franklin [Emp#2] => Array (  => Richard  => Narnie ) [Emp#3] => Jake [Emp#4] => Tyson [Emp#5] => Kylie ) */
Wonderful! It doesn’t overwrite but retains both the values in an array. Thanks to its ability to recursively merge the arrays. Next, we will see a more complex array set and how the array_merge_recursive function deals with it.
Usage Example#2 – PHP array_merge_recursive
<?php $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"]], ]; $merged_array = array_merge_recursive($array1, $array2, $array3); print_r($merged_array); ?>
Here are three arrays with repetitive information about users. We cannot afford to lose any information and want to accumulate the data in one array. So, array_merge_recursive kills two birds with one stone. Here’s the output.
/* OUTPUT Array ( [User#1] => Array ( [Status] => Active [Roles] => Array (  => Monitor  => Collaborator  => Developer )  => Hi! I am junior developer  => I develop the backend system ) [User#2] => Array ( [Status] => Offline [Roles] => Array (  => Developer  => QA )  => Hi! I am QA engineer  => I design the UI/UX of the application ) [User#3] => Array ( [Status] => Left [Roles] => Array (  => Trainee  => UI/UX  => Trainee  => UI/UX )  => Hi! I am UI/UX deisgner [MovedTo] => Project Zero ) [User#4] => Array ( [Status] => Active [Roles] => Array (  => Project Manager ) ) ) */
Voila! The array_merge_recursive accumulates all the users in one place and consistently integrates their data, whether the roles or the description strings, in one array. That’s an advantage over the array_merge in PHP in some cases.
PHP array_merge_recursive vs array_merge
Now, as we understand both the functions and see that the array_merge_recursive merges without overwrites, we wonder if array_merge is even viable because it can lose data to overwrites.
However, there is a crucial need for array_merge where we need overwrites. Such a scenario could be overwriting configuration files. Configuration files are an essential part of the development, and you will often find yourself tinkering with them. So, let’s emulate a scenario and use array_merge instead of recursive to overwrite the files.
<?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 ) */ ?>
See, the array_merge certainly has the edge over the array_merge_recursive in PHP. Imagine if we had used array_merge_recursive, we would have messed up the configuration file, which could be a disaster for the project. So, using the right function at the right time and place is the key to success in development.
In this article, we have explored PHP array_merge_recursive through examples and learned what’s different from the array_merge. Moreover, we have seen a scenario where array_merge is more viable than array_merge_recursive. We hope you’ve understood the new function. Stay tuned for more exciting PHP tutorials and articles.
Want to explore more useful PHP tutorials?
We have many fun articles related to PHP. You can explore these to learn more about PHP.