PHP array_merge_recursive Function | Merge Recursively

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 same key.

Syntax

array array_merge_recursive($array1, $array2, ……, $arrayn)

Parameters

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.

Return Value

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.

PHP array_merg_recursive Function Diagram

Examples

Let’s discuss a few examples of using array_merge_recursive function.

Example 1: Difference from array_merge Function

Let’s consider two associative arrays and merge them with array_merge function first.

<?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
	        (
	            [0] => ball
	            [1] => 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
	                (
	                    [0] => apple
	                    [1] => mango
	                )

	            [0] => banana
	        )

	    [0] => 15
	    [1] => 10
	)
	*/
?>

Conclusion

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.

Spread the Knowledge

Leave a Reply

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