Instance
Recursively replaces the value of the first array ($a 1) with the value of the second array ($a 2):
<?php$a1=array ("A" =>array ("Red"), "B" =>array ("green", "blue"), $a 2=array ("a" =>array ("Yellow"), "b" = >array ("Black"));p Rint_r (array_replace_recursive ($a 1, $a 2));? >
Definition and usage
The array_replace_recursive () function recursively replaces the value of the first array with the value of the subsequent array.
Tip: You can pass an array, or multiple arrays, to a function.
If a key exists in the first array array1 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 exists only in the first array array1, it will remain unchanged. If a key exists in the second array array2, but does not exist in the first array array1, the element is created in the first array array1. If more than one substitution array is passed, they are processed sequentially, and the values of the arrays are overwritten by the value of the previous array.
Note: If you do not specify a key for each array, the function behaves the same as the Array_replace () function.
Grammar
Array_replace_recursive (Array1,array2,array3 ...)
| parameters |
description |
| array1 |
required. Specifies an array. |
| array2 |
optional. Specifies an array of values to replace Array1. |
| optional. Specify multiple to replace Array1 and array2, ... An array of values. The value of the subsequent array overrides the value of the previous array. |
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:
<?php$a1=array ("A" =>array ("Red"), "B" =>array ("green", "Blue")), $a 2=array ("a" =>array ("Yellow"), "b" = >array ("Black")), $a 3=array ("a" =>array ("Orange"), "B" =>array ("Burgundy"));p Rint_r (array_replace_ Recursive ($a 1, $a 2, $a 3));? >
Example 2
The difference between array_replace () and 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 ($a 1, $a 2);p Rint_r ($ result); $result =array_replace ($a 1, $a 2);p Rint_r ($result);?