PHP array_replace_recursive() Function

The array_replace_recursive() function replaces the values of the first array with the values from following arrays recursively.

Tip: You can assign one array to the function, or as many as you like.

Note: If you do not specify a key for each array, this function will behave exactly the same as the array_replace() function.

Parameter Description
array1 Required. Specifies an array
array2 Optional. Specifies an array which will replace the values of array1
array3,… Optional. Specifies more arrays to replace the values of array1 and array2, etc. Values from later arrays will overwrite the previous ones.

Syntax

 array_replace_recursive(array1,array2,array3...) 

Example

$array1=array("a"=>array("Prince"),"b"=>array("James","Lachlan"),);
$array2=array("a"=>array("Alex"),"b"=>array("Cooper"));
print_r(array_replace_recursive($array1,$array2));

Output

Array ( [a] => Array ( [0] => Alex ) [b] => Array ( [0] => Cooper [1] => Lachlan ) ) 

Leave a Reply

Your email address will not be published. Required fields are marked *