PHP array_merge_recursive() Function
Last Updated :
20 Jun, 2023
The
array_merge_recursive() is an inbuilt function in PHP and is used to merge two or more arrays into a single array recursively. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the end of the previous array. If there are same keys in the given arrays, then the key is assigned a value which has an array that consists of the values with the same key.
Note: The function is different from
array_merge() in a way that in case of multiple arrays with same keys, array_merge() function takes the last array value from all the arrays, but in
array_merge_recursive() the key is assigned an array which consists of all the values of arrays which has the same key.
Syntax:
array_merge_recursive($array1, $array2, $array3...$arrayn)
Parameters: The function can take any number of arrays as parameter separated by comma (,) that we need to merge. The first parameter
$array1 is mandatory.
Return Value: The function returns a merged array which has all the arrays merged. The merging occurs in such a manner that the values of one array are appended at the end of the previous array. If there are same keys in the given arrays, then that key in the output array is assigned an array that consists of the values with the same key.
Examples:
Input : $a1=array("a"=>"raj", "b"=>"striver");
$a2=array("z"=>"geeks", "b"=>"articles");
Output :
Array
(
[a] => raj
[b] => Array
(
[0] => striver
[1] => articles
)
[z] => geeks
)
Explanation: "striver" and "articles" has the same
key "b", so the key b is assigned to an array which has
"striver" and "articles" as elements.
Input :$a1=array("a"=>"raj", "b"=>"striver");
$a2=array("z"=>"geeks", "d"=>"articles");
Output :
Array
(
[a] => raj
[b] => striver
[z] => geeks
[d] => articles
)
Below programs illustrate the array_merge_recursive() function:
Program 1: PHP program to demonstrate array_merge_recursive()
function with all different keys.
php
// PHP program to demonstrate array_merge_recursive()
// function with same keys
$a1=array("a"=>"raj", "b"=>"striver");
$a2=array("z"=>"geeks", "d"=>"articles");
print_r(array_merge_recursive($a1, $a2));
?>
Output:
Array
(
[a] => raj
[b] => striver
[z] => geeks
[d] => articles
)
Program 2: PHP program to demonstrate array_merge_recursive() function with same keys.
php
// PHP program to demonstrate array_merge_recursive()
// function with same keys
$a1=array("a"=>"raj", "b"=>"striver");
$a2=array("z"=>"geeks", "b"=>"articles");
//function call
print_r(array_merge_recursive($a1, $a2));
?>
Output:
Array
(
[a] => raj
[b] => Array
(
[0] => striver
[1] => articles
)
[z] => geeks
)
Reference:
http://php.net/manual/en/function.array-merge-recursive.php
Similar Reads
PHP array_chunk() Function The array_chunk() function is an inbuilt function in PHP which is used to split an array into parts or chunks of given size depending upon the parameters passed to the function. The last chunk may contain fewer elements than the desired size of the chunk. Syntaxarray array_chunk( $array, $size, $pre
2 min read
PHP array_combine() Function The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of t
2 min read
PHP array_count_values() Function The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi
2 min read
PHP array_diff_assoc() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares both the keys and values between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to there keys and values and returns the
2 min read
PHP array_diff_key() Function This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares the keys between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to their keys and returns the elements that are present
2 min read
PHP array_diff_uassoc() Function The array_diff_uassoc() function is a built-in function in PHP and is used to get the difference between one or more arrays using an user-defined function to compare the keys. This function compares both the keys and values between one or more arrays to and returns the elements from the first array
4 min read
PHP array_diff_ukey() Function The array_diff_ukey() function is an inbuilt function in PHP. It is used to compares the key two or more arrays using a user-defined function, and return an array, which is array1 and its not present any others array2, array3 or more... Syntax: array_diff_ukey($array1, $array2, $array3..., arr_diffu
4 min read
PHP array_diff() function The array_diff() is an inbuilt function in PHP ans is used to calculate the difference between two or more arrays. This function computes difference according to the values of the elements, between one or more array and return differences in the form of a new array. This function basically returns a
2 min read
PHP array_fill_keys() Function The array_fill_keys() function is a builtin function in PHP and is used to create a new array filled with the given keys and value provided as an array to the function. Syntax: array array_fill_keys ( $keys, $value ) Parameters: This function accepts two parameters, keys and their values to be prese
3 min read
PHP array_fill() function The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value. Syntax: array_fill($start_index, $number_elements, $values) Parameter: The array_fill() function takes three parameters and ar
2 min read