Master PHP’s array_replace_recursive: Recursive Array Merging Explained

This article explains PHP’s array_replace_recursive function, detailing its syntax, recursive merging behavior, key‑overriding rules, and differences from array_merge_recursive, accompanied by clear code examples and output to help developers efficiently combine multidimensional arrays.

php Courses
php Courses
php Courses
Master PHP’s array_replace_recursive: Recursive Array Merging Explained

PHP is a popular web programming language with a rich function library; among them 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 and returns a merged array. It recursively compares keys and values; when keys match, their values are merged recursively. If a value is itself an array, the merge continues until no deeper 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 matching keys of $array1 while leaving other keys untouched, making array merging more convenient.

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 and only add new ones from the source array, you can use array_merge_recursive(), which behaves similarly but does not overwrite existing values.

In summary, array_replace_recursive() is a powerful function for recursively merging two or more arrays, allowing you to control whether keys are overwritten or retained, and is highly useful when handling complex PHP array structures.

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.

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