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

This article explains PHP's array_replace_recursive() function, detailing its syntax, recursive merging behavior, example code, output interpretation, and important usage notes, while comparing it with array_merge_recursive for handling array key conflicts in PHP development.

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

PHP is a popular web programming language with a rich function library; the array_replace_recursive() function merges one or more arrays recursively, combining their keys, values, and sub‑arrays.

The basic syntax is:

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

The 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, the merge continues until no sub‑arrays remain.

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 resulting output is:

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

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

The output shows that $array2 recursively overwrote the corresponding keys of $array1, while other keys remained unchanged, demonstrating how the function can conveniently merge 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 prefer to keep existing keys in the target array and only add missing keys from the source, you can use array_merge_recursive(), which behaves similarly but does not overwrite existing values.

In summary, array_replace_recursive() is a practical function for recursively merging arrays in PHP, allowing you to control whether keys and values are overwritten or retained during the merge.

The article also provides links to download learning materials for Java, C, front‑end development, C++, and PHP.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCode Examplearray_replace_recursiverecursive merge
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

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.