PHP | array_merge_recursive() Function

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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));
?>

chevron_right


Output:

Array
(
    [a] => raj
    [b] => striver
    [z] => geeks
    [d] => articles
)

Program 2: PHP program to demonstrate array_merge_recursive() function with same keys.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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));
?>

chevron_right


Output:

Array
(
    [a] => raj
    [b] => Array
        (
            [0] => striver
            [1] => articles
        )

    [z] => geeks
)

Reference:
http://php.net/manual/en/function.array-merge-recursive.php



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.