PHP Function Reference

PHP array_replace_recursive() Function



The PHP array_replace_recursive() function replaces the values of first array with values having the same keys in each of the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If a key only exists in the second array, it will be created in the first array. If a key only exists in the first array, it will be left as it is. If multiple arrays are used, values from later arrays will overwrite the previous ones.

This function recurses into deeper arrays. When the value in the first array is scalar, it will be replaced by the value in the second array, it can be scalar or array. When the value in the first array and the second array are both arrays, this function will replace their respective value recursively.

Syntax

array_replace_recursive(array, replacements)

Parameters

array Required. Specify the array in which elements are replaced.
replacements Required. Specify arrays from which elements will be extracted.

Return Value

Returns an array, or null if an error occurs.

Exceptions

NA.

Example:

The example below shows the usage of array_replace_recursive() function.

<?php
$Arr1 = array("a"=>"Red", "b"=>"Green", "c"=>"Blue",
              "others"=>array("x"=>"White", "y"=>"Black"));
$Arr2 = array("a"=>"Crimson",
              "others"=>array("y"=>"Yellow"));

$result = array_replace_recursive($Arr1, $Arr2);

print_r($result);
?>

The output of the above code will be:

Array
(
    [a] => Crimson
    [b] => Green
    [c] => Blue
    [others] => Array
        (
            [x] => White
            [y] => Yellow
        )
)

Example:

The example below shows the result when keys only exists in either of arrays but not in both.

<?php
$Arr1 = array("a"=>"Red", "b"=>"Green", "c"=>"Blue",
              "others"=>array("x"=>"White", "y"=>"Black"));
$Arr2 = array("d"=>"Crimson",
              "others"=>array("z"=>"Yellow"));

$result = array_replace_recursive($Arr1, $Arr2);

print_r($result);
?>

The output of the above code will be:

Array
(
    [a] => Red
    [b] => Green
    [c] => Blue
    [others] => Array
        (
            [x] => White
            [y] => Black
            [z] => Yellow
        )

    [d] => Crimson
)

Example:

Consider the example below where multiple arrays are used for replacement and values from later arrays will overwrite the previous ones.

<?php
$Arr1 = array("a"=>"Red", "b"=>"Green", "c"=>"Blue",
              "others"=>array("x"=>"White", "y"=>"Black"));
$Arr2 = array("a"=>"Crimson",
              "others"=>array("y"=>"Yellow"));
$Arr3 = array("others"=>array("y"=>"Pink"));

$result = array_replace_recursive($Arr1, $Arr2, $Arr3);
print_r($result);
?>

The output of the above code will be:

Array
(
    [a] => Crimson
    [b] => Green
    [c] => Blue
    [others] => Array
        (
            [x] => White
            [y] => Pink
        )
)

❮ PHP Array Reference