Home >>PHP Array Functions >PHP array_replace_recursive() Function
PHP array_replace_recursive() function is used to replace the values of the first array with the values from following arrays recursively.
It accepts a list of arrays as input parameters where the first parameter is compulsory and the rest are optional. It returns the new modified array or NULL if an error occurs.
Syntax:
array_replace_recursive($array1, $array2, $array3...);
Parameter | Description |
---|---|
array1 | This is a required parameter. This parameter defines an array. |
array2 | This is an optional parameter. This parameter defines an array which will replace the values of array1. |
array3,... | This is an optional parameter. This parameter defines more arrays to replace the values of array1 and array2. |
Here is an example of array_replace_recursive() function in PHP:
<html> <body> <pre> <?php $a=array("a","b","c","d","e","f","g"); $x=array("u","v","w","x","y","z"); print_r(array_replace_recursive($a,$x)); ?> </pre> </body> </html>
Array ( [0] => u [1] => v [2] => w [3] => x [4] => y [5] => z [6] => g )
Example 2:
<html> <body> <pre> <?php $x=array("a"=>array("Abhi"),"b"=>array("Java","Python"),); $y=array("a"=>array("Jerry"),"b"=>array("PHP")); $z=array("b"=>array("JavaScript")); print_r(array_replace_recursive($x,$y)); echo "<br>"; print_r(array_replace_recursive($y,$x,$z)); ?> </pre> </body> </html>
Array ( [a] => Array ( [0] => Jerry ) [b] => Array ( [0] => PHP [1] => Python ) ) Array ( [a] => Array ( [0] => Abhi ) [b] => Array ( [0] => Javascript [1] => Python ) )