PHP array_replace_recursive() Function (original) (raw)

Last Updated : 11 Jul, 2025

The array_replace_recursive() is an inbuilt function in PHP that replaces the values of the first array with the values from following arrays recursively. It performs the replacement based on the below rules:

Syntax:

array_replace_recursive($array1, array2,array2, array2,array3...)

Parameters: This function accepts a list of arrays as parameters where the first parameter is compulsory and rest are optional.Return Value: It returns the modified array, or NULL if an error occurs.Example:

Input: $array1 = array("a"=>array("red"), "b"=>array("green")); $array2 = array("a"=>array("yellow"), "b"=>array("black")); Output: Array ( [a] => Array ( [0] => yellow ) [b] => Array ( [0] => black ) )

Below program illustrate the array_replace_recursive() function:

PHP `

array("red"), "b" => array("green", "blue")); $array2=array( "a" => array("yellow"), "b" => array("black")); $array3=array("a" => array("orange"), "b" => array("burgundy")); print_r(array_replace_recursive($array1, array2,array2, array2,array3)); ?>

`

Output:

Array ( [a] => Array ( [0] => orange )

[b] => Array
    (
        [0] => burgundy
        [1] => blue
    )

)

Reference:https://www.php.net/manual/en/function.array-replace-recursive.php