Backend Development 4 min read

Using PHP's array_replace_recursive() Function to Merge Arrays Recursively

This article explains the PHP array_replace_recursive() function, its syntax, recursive merging behavior, example usage with code snippets, output interpretation, important considerations, and a comparison with array_merge_recursive() for effective backend array handling.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's array_replace_recursive() Function to Merge Arrays Recursively

PHP is a popular web programming language with a rich function library that helps us handle various tasks. Among them, the array_replace_recursive() function is used to merge one array with another or multiple arrays, recursively combining their key‑value pairs and sub‑arrays. This article introduces how to use this function.

The basic syntax of array_replace_recursive() is as follows:

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

This function accepts multiple arrays as parameters and returns the merged array. The arrays are merged recursively, meaning it recursively compares keys and values. If two keys match, their values are merged recursively. If a value is an array, that array is merged recursively until there are no sub‑arrays.

Below is an example:

$array1 = array(
    'fruit' => array(
        'apple' => 1,
        'orange' => 4,
        'banana' => 3
    ),
    'vegetable' => array(
        'potato' => 2,
        'broccoli' => 1,
        'carrot' => 4
    )
);

$array2 = array(
    'fruit' => array(
        'orange' => 2
    ),
    'vegetable' => array(
        'potato' => 3,
        'broccoli' => 2,
        'carrot' => 1
    )
);

$result = array_replace_recursive($array1, $array2);

print_r($result);

The output is as follows:

Array
(
    [fruit] => Array
        (
            [apple] => 1
            [orange] => 2
            [banana] => 3
        )

    [vegetable] => Array
        (
            [potato] => 3
            [broccoli] => 2
            [carrot] => 1
        )

)

As you can see, the array $array2 recursively overwrote the corresponding keys of $array1 , while other keys remained unaffected. Using this function makes merging arrays more convenient.

Note that when using array_replace_recursive() , if the same key appears in multiple arrays, later arrays will overwrite earlier ones. Array keys must be strings or integers; otherwise a warning is generated.

Additionally, if you want to keep keys and values that exist in the target array while adding keys and values from the source array that do not exist, you can use array_merge_recursive() . This function is similar to array_replace_recursive() , except the latter overwrites existing keys and values.

In summary, the array_replace_recursive() function is very useful. It can recursively merge two or more arrays and overwrite or retain existing keys and values as desired. If you need to merge PHP arrays, this function is worth using.

backendPHPrecursionarray_replace_recursivearray merging
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.