Example
Recursively replace the value of the first array ($a1) with the value of the second array ($a2):
<?php $a1=array("a"=>array("red"),"b"=>array("green","blue"),); $a2=array("a"=>array("yellow"),"b"=>array("black")); print_r(array_replace_recursive($a1,$a2)); ?>
Definition and usage
array_replace_recursive() function recursively replaces the value of the first array with the value of the following array.
Tip: You can pass an array, 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 the first array array1 will be replaced by the value in the second array array2. If a key only exists in the first array array1, it will remain unchanged. If a key exists in the second array, array2, but not in the first array, array1, the element will be created in the first array, array1. If multiple replacement arrays are passed, they will be processed in order, and the values of subsequent arrays will overwrite the values of previous arrays.
Note : If a key is not specified for each array, the behavior of this function will be equivalent to the array_replace() function.
Syntax
array_replace_recursive(array1,array2,array3...)
Parameters | Description |
array1 | Required. Specify an array. |
array2 | Optional. Specifies an array to replace the value of array1 . |
array3,... | Optional. Specify multiple arrays to replace the values of array1 and array2, ... . The values of the following array will overwrite the values of the previous array. |
Technical details
Return value: | Returns the replaced array if an error occurs Then return NULL. |
PHP version: | 5.3.0+ |
更多实例
实例 1
多个数组:
<?php $a1=array("a"=>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)); ?>
实例 2
array_replace() 与 array_replace_recursive() 之间的不同:
<?php $a1=array("a"=>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); ?>
The above is the detailed content of PHP recursively replaces the value of the first array with the value of the following array function array_replace_recursive(). For more information, please follow other related articles on the PHP Chinese website!