Combine two arrays into a single array:
<?php $a1=array(“a”=>“red”,“b”=>“green”); $a2=array(“c”=>“blue”,“b”=>“yellow”); print_r(array_merge_recursive($a1,$a2)); ?> |
The array_merge_recursive() function combines one or more arrays into a single array.
Unlike array_merge(), which overrides values for duplicate keys, array_merge_recursive() merges values into an array if the keys are the same.
Note: When only one array is passed to array_merge_recursive(), it behaves the same as array_merge().
array_merge_recursive(array1, array2, array3, …) |
Parameter |
Description |
array1 |
Required. Defines an array. |
array2 |
Optional. Provides an array. |
array3,… |
Optional. Indicates an array. |
Return Value: |
Returns the combined array. |
PHP Version: |
4.0.1+ |