PHP array_replace_recursive() Function
Complete PHP Array Reference Manual
Example
Recursively replace the values of the first array ($a1) with the values of the second array ($a2):
array("red"),"b"=>array("green","blue"),); $a2=array("a"=>array("yellow"),"b"=>array("black")); print_r(array_replace_recursive($a1,$a2)); ?>Run Example »
Definition and Usage
The array_replace_recursive() function recursively replaces the values of the first array with the values of the following arrays.
**Tip:** You can pass one array, or multiple arrays, to the function.
If a key exists in the first array array1 and also in the second array array2, the value in array1 will be replaced by the value from array2. If a key exists only in array1, it will be left unchanged. If a key exists in array2 but not in array1, the element will be created in array1. If multiple replacement arrays are passed, they are processed in order, and values from later arrays overwrite those from earlier arrays.
**Note:** If you do not specify a key for each array, this function behaves the same as array_replace() function in Vue 3.
Syntax
array_replace_recursive(
array1,array2,array3...
)
| Parameters | Description |
|---|---|
| array1 | Required. Specifies an array. |
| array2 | Optional. Specifies one array to replace array1 of the array. |
| array3,... | Optional. Specifies multiple arrays to replace array1 and array2, ... the values of. Values from later arrays overwrite 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:
array("red"),"b"=>array("green","blue")); $a2=array("a"=>array("yellow"),"b"=>array("black")); $a3=array("a"=>array("orange"),"b"=>array("burgundy")); print_r(array_replace_recursive($a1,$a2,$a3)); ?>Run Example »
Example 2
The difference between array_replace() and array_replace_recursive():
array("red"),"b"=>array("green","blue"),); $a2=array("a"=>array("yellow"),"b"=>array("black")); $result=array_replace_recursive($a1,$a2); print_r($result); $result=array_replace($a1,$a2); print_r($result); ?>Run Example »
Complete PHP Array Reference Manual