Master PHP’s array_replace_recursive: Recursive Array Merging Made Easy

This article explains PHP’s array_replace_recursive function, detailing its syntax, recursive merging behavior, key‑overriding rules, and provides step‑by‑step code examples that demonstrate how multiple arrays are combined, along with tips on when to use array_merge_recursive instead.

php Courses
php Courses
php Courses
Master PHP’s array_replace_recursive: Recursive Array Merging Made Easy

PHP is a popular web programming language with a rich function library. The array_replace_recursive() function merges one array with one or more other arrays recursively, combining their key‑value pairs and sub‑arrays.

The basic syntax is:

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

This function accepts multiple arrays as arguments and returns the merged array. It recursively compares keys and values; when keys match, their values are merged. If a value is an array, it is merged recursively 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 output is:

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

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

As shown, $array2 recursively overwrites the corresponding keys of $array1, while other keys remain unaffected. This function makes array merging more convenient.

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

If you want to keep existing keys and add new ones from the source array, you can use array_merge_recursive(). Unlike array_replace_recursive(), it does not overwrite existing keys.

In summary, array_replace_recursive() is a very useful function for recursively merging two or more arrays in PHP, allowing you to control whether keys and values are overwritten or retained.

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.

Code 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.