PHP array_replace_recursive()
Function
Complete PHP Array Reference Manual
Example
Recursively replace values of the first array ($a1) with values from the second array ($a2):
Definition and Usage
The array_replace_recursive()
function recursively replaces values of the first array with values from subsequent arrays.
Tip: You can pass one or multiple arrays to the function.
If a key exists in the first array array1
and also exists in the second array array2
, the value in array1
will be replaced by the value in array2
. If a key exists only in array1
, it will remain unchanged. If a key exists in array2
but not in array1
, it will be created in array1
. If multiple replacement arrays are passed, they will be processed sequentially, with values from later arrays overriding those from earlier arrays.
Note: If no key is specified for each array, the function behaves the same as the array_replace() function.
Syntax
Parameter | Description |
---|---|
array1 | Required. Specifies an array. |
array2 | Optional. Specifies an array to replace values of array1 . |
array3,... | Optional. Specifies multiple arrays to replace values of array1 , array2 , etc. Values from later arrays will override those from earlier arrays. |
Technical Details
Return Value: | Returns the replaced array, or NULL if an error occurs. |
---|---|
PHP Version: | 5.3.0+ |
--- | --- |
More Examples
Example 1
Multiple arrays:
Example 2
Difference between array_replace()
and array_replace_recursive()
: