Master PHP’s array_replace_recursive: Recursive Array Merging Made Easy

This guide explains how the PHP array_replace_recursive() function recursively merges multiple arrays, demonstrates its syntax and behavior with clear code examples, and highlights important considerations and alternatives for effective backend array handling.

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, including their key‑value pairs and subarrays. This article introduces its usage.

The basic syntax is:

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

The function accepts multiple arrays as parameters and returns the merged array. Arrays are merged recursively, meaning keys and values are compared recursively. If two keys match, their values are merged recursively. If a value is an array, it is merged recursively until no subarrays remain.

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
        )
)

$array2 recursively overwrites the corresponding keys of $array1, while other keys remain unaffected, making array merging more convenient.

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 values in the target array while adding keys/values from the source array that do not exist, you can use array_merge_recursive(). This function is similar, but array_replace_recursive() overwrites existing keys and values.

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

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.

BackendPHPfunctionsarray mergeRecursive
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.