YouTip LogoYouTip

Func Array Replace Recursive

# PHP array_replace_recursive() Function [![Image 3: PHP Array Reference Manual](#)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 subsequent arrays. **Tip:** You can pass one array to the function, or multiple arrays. If a key exists in the first array array1 and also 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 not in the first array array1, this element will be created in the first array array1. If multiple replacement arrays are passed, they will be processed in order, with the values of later arrays overriding the values of earlier arrays. **Note:** If no keys are specified for each array, the function will behave like the [array_replace()](#) function. * * * ## Syntax array_replace_recursive(_array1,array2,array3..._) | Parameter | Description | | --- | --- | | _array1_ | Required. Specifies an array. | | _array2_ | Optional. Specifies an array whose values will replace the values of _array1_. | | _array3,..._ | Optional. Specifies multiple arrays whose values will replace the values of _array1_ and _array2, ..._. The values of later arrays will override the values of 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 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](#)
← Php Magic ConstantFunc Array Replace β†’