Backend Development 4 min read

Using PHP's array_replace_recursive() to Recursively Merge Arrays

This article explains the PHP function array_replace_recursive(), showing its syntax, behavior, and examples of how it recursively merges multiple arrays while handling key conflicts and offering guidance on when to use it versus array_merge_recursive().

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's array_replace_recursive() to Recursively Merge Arrays

PHP is a popular web programming language with a rich function library that can help us handle various tasks. Among them, the array_replace_recursive() function is used to merge one array with another or multiple arrays, recursively combining two or more arrays, including their key‑value pairs and sub‑arrays. This article introduces how to use this function.

The basic syntax of array_replace_recursive() is as follows:

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

This function accepts multiple arrays as parameters, and its return value is the merged array. The arrays are merged recursively, meaning it recursively compares the keys and values of the arrays. If two keys match, their values are merged recursively. If a value is an array, that array is merged recursively until there are no sub‑arrays left.

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
        )

)

As you can see, the array $array2 recursively overwrites the corresponding keys of $array1 , while other keys remain unaffected. Using this function makes array merging more convenient.

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

Additionally, if you want to keep the keys and values that exist in the target array while adding keys and values from the source array that do not exist in the target, you can use array_merge_recursive() . This function is similar to array_replace_recursive() , except that the latter overwrites existing keys and values.

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

backendPHParraysarray_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

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.