array_merge_recursive()
array_merge_recursive() — Merge one or more arrays recursively.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
Signature
array_merge_recursive(array ...$arrays): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| ...$arrays | array | Variable list of arrays to recursively merge. |
Return value
An array of values resulted from merging the arguments together. If called without any arguments, returns an empty array.
Example
A runnable snippet:
$ar1 = array("color" => array("favorite" => "red"), 5); $ar2 = array(10, "color" => array("favorite" => "green", "blue")); $result = array_merge_recursive($ar1, $ar2); print_r($result);
Output:
Array ( [color] => Array ( [favorite] => Array ( [0] => red [1] => green ) [0] => blue ) [0] => 5 [1] => 10 )
Changelog
| Version | Change |
|---|---|
| 7.4.0 | This function can now be called without any parameter. Formerly, at least one parameter has been required. |
See also
array_merge(), array_replace_recursive()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_merge_recursive() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.