PHP array_merge_recursive() Function

Example

Merge two arrays into one array:

<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2));
?>

Running Example

Definition and Usage

The array_merge_recursive() function merges one or more arrays into a single array.

This function is similar to array_merge() The difference between the functions lies in how they handle the case where two or more array elements have the same key name. array_merge_recursive() does not perform key overwrite, but recursively combines the values with the same key name into an array.

Note:If you only pass an array to the array_merge_recursive() function, the result is the same as array_merge(), and the function will return a new array with integer keys, which are re-indexed starting from 0.

Syntax

array_merge_recursive(array1,array2,array3...)
Parameter Description
array1 Required. Specify an array.
array2 Optional. Specify an array.
array3 Optional. Specify an array.

Technical Details

Return Value: Returns the merged array.
PHP Version: 4.0.1+