PHP array_merge_recursive is an inbuilt Function in PHP. It merges two or more arrays to a single array recursively. The function merges arrays in such a manner, that it appends the values of one array at the end of the previous array. Also, if there are the same keys in the arrays, it assigns the value to the key which has an array that consists of the values with the same kehay. In this article, we will discuss the PHP array_merge_recursive Function.
Note: This function is different than the array_merge Function. In case of multiple arrays with the same keys, the array_merge takes the last value from all the arrays. However, array_merge_recursive assigns an array to that key with values from all the arrays with the
array array_merge_recursive($array1, $array2, ……, $arrayn)
The PHP array_merge_recursive Function takes in a list of array parameters. You can pass any number of parameters in the function. However, all the parameters must be arrays. The first array is mandatory.
The PHP array_merge_recursive function returns an array after merging all the input arrays. While merging, it appends values from one array at the back of the previous array. However, if multiple arrays have the same key, then the key in the output array consists of an array with values from all the arrays with the same key.
Let’s discuss a few examples of using array_merge_recursive function.
Example 1: Difference from array_merge Function
<?php $array1 = array('a' => 'apple', 'b' => 'ball'); $array2 = array('c' => 'cat', 'b' => 'balloon'); $mergedArray = array_merge($array1, $array2); print_r($mergedArray); /* Array ( [a] => apple [b] => balloon [c] => cat ) */ ?>
You can observe, both the arrays have ‘b’ key in common. However, in the output array, the value at the key ‘b’ is the value from the second array (balloon). Now, let’s merge using PHP array_merge_recursive Function.
<?php $array1 = array('a' => 'apple', 'b' => 'ball'); $array2 = array('c' => 'cat', 'b' => 'balloon'); $mergedArray = array_merge_recursive($array1, $array2); print_r($mergedArray); /* Array ( [a] => apple [b] => Array (  => ball  => balloon ) [c] => cat ) */ ?>
Now, in the output array, the key ‘b’ has an array with values from both the arrays.
Example 2: Nested Arrays
Similarly, we can also pass nested arrays in the function.
<?php $array1 = array('fruit' => array('favorite' => 'apple'), 15); $array2 = array(10, 'fruit' => array('favorite' => 'mango', 'banana')); $result = array_merge_recursive($array1, $array2); print_r($result); /* Array ( [fruit] => Array ( [favorite] => Array (  => apple  => mango )  => banana )  => 15  => 10 ) */ ?>
In conclusion, we discussed the PHP array_merge_recursive Function in this article. You can read more about it on the Official Documentation of PHP. Also, you can learn about more PHP Array Functions on Concatly.
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.