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

Run Example

Definition and Usage

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

This function is similar to array_merge() The difference between the function lies in the handling of two or more arrays with the same key name. array_merge_recursive() will not overwrite the key names, but will recursively combine the values with the same key names into an array.

Note:If you only input an array into 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 array.
array2 Optional. Specify array.
array3 Optional. Specify array.

Technical Details

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