PHP array_merge_recursive() Function

What is PHP array_merge_recursive() Function?

If you want to merge multiple arrays recursively together to make one array, use array_merge_recursive() function. In the resulting array, it adds one array to the end of previous array. The function can combine values of both indexed and associative array or even mix of both.

Even you can merge one array or an empty array recursively. You’ll learn detail about this in the coming sections.

Why the merging process is called recursive?

If multiple arrays have same ”string key”, the function creates a new array with those values, instead of overwriting one value with others. If the nested array has further matching “string keys”, the function continues repeating the process. Check example 1.

Syntax:

array_merge_recursive(array1, array2, … arrayN)

Parameters:

The Function has 1 required parameter and zero or more optional parameters-

array1 (Required): It is an array. It could be an indexed or associative array or an empty array.

array2, …arrayN (Optional): It could be one array or any number of arrays. Each array could be an indexed or associative array or an empty array.

Return Values:

The function returns the merged array. The function applies the following rules while merging arrays-

  • The merged array contains the elements of the first array, followed by the elements of the next array, and so on. Remember this while applying the other rules. Check the example below-
    <pre>
    <?php
    $arr1= ["HTML","CSS","Javascript"];
    $arr2= ["PHP","MySQL","Ajax"];
    $mergArr=array_merge_recursive($arr1,$arr2);
    print_r($mergArr);
    ?>
    </pre>
    

    Output:

    Array
    (
    [0] => HTML
    [1] => CSS
    [2] => Javascript
    [3] => PHP
    [4] => MySQL
    [5] => Ajax
    )

    Explanation:

    The function merges all the elements of two arrays one after another.

  • If the input arrays have same string keys, the function creates a new array with those values.  Check the example below-
    <pre>
    <?php
    $arr1= ["One"=>"HTML","Two"=>"CSS","Three"=>"JavaScript"];
    $arr2= ["One"=>"PHP","Two"=>"MySQL","Four"=>"Ajax"];
    $mergArr=array_merge($arr1,$arr2);
    print_r($mergArr);
    ?>
    </pre>
    

    Output:

    Array
    (
    [One] => Array
    (
    [0] => HTML
    [1] => PHP
    )

    [Two] => Array
    (
    [0] => CSS
    [1] => MySQL
    )

    [Three] => JavaScript
    [Four] => Ajax
    )

    Explanation:

    As two arrays have same string keys “One”, the function creates a new array with those values. Same happens to key “Two”.

  • If the input arrays have same numeric keys, the function doesn’t override the previous value by the later one or vice versa rather appends. And the numeric keys of the resultant array are renumbered starting from 0 and increases by 1. Check the example below-
    <pre>
    <?php
    $arr1= [0=>"HTML",1=>"CSS",10=>"JavaScript"];
    $arr2= [0=>"PHP",1=>"MySQL",6=>"Ajax"];
    $mergArr=array_merge_recursive($arr1,$arr2);
    print_r($mergArr);
    ?>
    </pre>
    

    Output:

    Array
    (
    [0] => HTML
    [1] => CSS
    [2] => JavaScript
    [3] => PHP
    [4] => MySQL
    [5] => Ajax
    )

    Explanation:

    When the function finds numeric keys in any array, it renumbers from 0 and increments by 1 for the subsequent numeric key values.

  • If the arrays are mix of both numeric and string keys, the function follows the numeric key rules discussed above for the numeric key based values and applies string key rules for string keys based values. Check the example below-
    <pre>
    <?php
    $arr1= ["Language"=>"HTML",5,1993,"Element"=>"Yes","Many"];
    $arr2= ["Language"=>"PHP",8.3,1994,"Element"=>"No",1200 ];
    $mergArr=array_merge_($arr1,$arr2);
    print_r($mergArr);
    ?>
    </pre>
    

    Output:

    Array
    (
    [Language] => Array
    (
    [0] => HTML
    [1] => PHP
    )

    [0] => 5
    [1] => 1993
    [Element] => Array
    (
    [0] => Yes
    [1] => No
    )

    [2] => Many
    [3] => 8.3
    [4] => 1994
    [5] => 1200
    )

    Explanation:

    In case of the same string keys ex. “Language”. “Element” the function creates new arrays with those values. As the remaining elements of the two arrays have numeric keys, the function renumbers the keys from zero and increments each keys by one.

  • If you use only one numeric key array, in the merged array the integer keys start at 0 and increase by one. In case of string key array only, no change occurs. Check the example below-
    <pre>
    <?php
    $arr1= [0=>"HTML",1=>"CSS",10=>"Javascript"];
    $mergArr=array_merge_recursive($arr1);
    print_r($mergArr);
    echo"<br />";
    $arr= ["one"=>"PHP",
        "two"=>"MySQL",
        "four"=>"Python",
        "three"=>"JavaScript"
    ];
    $mergArr=array_merge_recursive($arr);
    print_r($mergArr);
    ?>
    </pre>
    

    Output:

    Array
    (
    [0] => HTML
    [1] => CSS
    [2] =>Javascript
    )

    Array
    (
    [one] => PHP
    [two] => MySQL
    [four] => Python
    [three] => JavaScript
    )

    Explanation:

    When array_merge_recursive() takes one numeric key array only, it just renumbers the keys from 0 and increases each value by one. On the other hand, in case of an array with just string keys only, it does nothing.

  • If you don’t use any array or in other word calling the function without any parameter, the function returns an empty array. Check the example below-
    <pre>
    <?php
    $mergArr=array_merge_recursive();
    print_r($mergArr);
    ?>
    </pre>
    

    Output:

    Array
    (
    )
  • Instead of an array, if you want to use a non-array type value, apply type casting on it and the function converts it to an array element. The function numbers it with an incrementing numeric key, or 0 if it the first parameter. Check the example below-
    <pre>
    <?php
    $arr1= (array) "String";
    $arr2= [0=>"PHP",1=>"MySQL"];
    $arr3= (array) TRUE;
    $arr4= (array) 100;
    $mergArr=array_merge_recursive($arr1,$arr2,$arr3,$arr4);
    print_r($mergArr);
    ?>
    </pre>
    

    Output:

    Array
    (
    [0] => String
    [1] => PHP
    [2] => MySQL
    [3] => 1
    [4] => 100
    )

    Explanation:

    As the “String”, “PHP”, and “MySQL” are string, Boolean, and integer type values respectively and these are non-array type values, we type cast on these with (array), then we use those in the function. The function set numeric keys for those starting with 0 and increments by 1.

Caution:

Even you quote numeric keys, the function considers it numeric keys, not string keys. Check the example below-

<pre>
<?php// #1
$arr1= ["0"=>"HTML","1"=>"CSS","10"=>"Javascript"];
$arr2= ["0"=>"PHP","1"=>"MySQL","6"=>"Ajax"];
$mergArr=array_merge($arr1,$arr2);
print_r($mergArr);
?>
</pre>

Output:

Array
(
[0] => HTML
[1] => CSS
[2] =>Javascript
[3] => PHP
[4] => MySQL
[5] => Ajax
)

PHP Version Support:

PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8

Summary: PHP array_merge_recursive() Function

The array_merge_recursive() function is a built-in PHP function and part of the PHP’s array functions. If you want to recursively merge two or more arrays, use array_merge_recursive() function.

Reference:

https://www.php.net/manual/en/function.array-merge-recursive.php