Backend Development 4 min read

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

This article explains PHP's array_replace_recursive() function, detailing its syntax, recursive merging behavior, example usage with code snippets, output interpretation, and important considerations such as key overwriting and differences from array_merge_recursive(), providing developers with a practical guide for combining arrays.

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, and the array_replace_recursive() function is used to merge one or more arrays recursively, combining their keys, values, and sub‑arrays.

The basic syntax of the function is:

<code>array_replace_recursive(array1, array2, array3......);</code>

This function accepts multiple arrays as arguments and returns a merged array. It recursively compares keys and values; when keys match, their values are merged recursively, and if a value is itself an array, that sub‑array is merged until no deeper arrays remain.

Below is an example demonstrating its usage:

<code>$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);
</code>

The output of the above code is:

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

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

)</code>

As shown, the keys in $array2 recursively overwrite the corresponding keys in $array1 , while other keys remain unchanged, making the function convenient for merging arrays.

Note that when the same key appears in multiple arrays, later arrays overwrite earlier ones, and array keys must be strings or integers; otherwise a warning is generated.

If you want to keep existing keys and values in the target array while adding new ones from the source, you can use array_merge_recursive() . This function behaves similarly but does not overwrite existing keys and values like array_replace_recursive() does.

In summary, array_replace_recursive() is a very useful function that recursively merges two or more arrays, allowing you to control whether keys and values are overwritten or retained, and is worth using when you need to combine PHP arrays.

BackendPHParrayrecursionphp-functions
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.