PHP array_merge_recursive() function


The array_merge_recursive() function in PHP merges two or more arrays into one, similarly to array_merge(), but with an important difference: if two arrays have the same string key, instead of overwriting the values, array_merge_recursive() combines the values into an array. Numeric keys, however, are still appended and re-indexed like in array_merge().

This function is particularly useful when you want to merge complex arrays (e.g., multidimensional arrays) and retain all values rather than having them overwritten.

Syntax:

array_merge_recursive(array ...$arrays): array

Parameters:

  • $arrays: One or more arrays to merge. You can pass multiple arrays, and they will all be recursively merged.

Return Value:

  • Returns a new array with merged values. If two or more arrays have the same string key, their values are merged into an array. For numeric keys, values are appended.

Example 1: Basic Usage

When two arrays with different string keys are merged, array_merge_recursive() behaves similarly to array_merge().

<?php $array1 = ["name" => "Alice", "age" => 25]; $array2 = ["email" => "alice@example.com"]; $result = array_merge_recursive($array1, $array2); print_r($result); ?>

Output:

Array ( [name] => Alice [age] => 25 [email] => alice@example.com )

Here, both arrays have different keys, so they are simply combined into a single array.

Example 2: Merging Arrays with the Same String Key

When two arrays have the same string key, their values are combined into an array.

<?php $array1 = ["name" => "Alice", "hobbies" => "reading"]; $array2 = ["name" => "Bob", "hobbies" => "swimming"]; $result = array_merge_recursive($array1, $array2); print_r($result); ?>

Output:

Array ( [name] => Array ( [0] => Alice [1] => Bob ) [hobbies] => Array ( [0] => reading [1] => swimming ) )

In this example, since both arrays contain the "name" and "hobbies" keys, the values are combined into arrays. The value "Alice" from the first array and "Bob" from the second array are placed in a new array under the "name" key.

Example 3: Merging Nested Arrays

array_merge_recursive() works recursively, meaning it will merge arrays at all levels of depth.

<?php $array1 = [ "person" => [ "name" => "Alice", "age" => 25 ] ]; $array2 = [ "person" => [ "name" => "Bob", "email" => "bob@example.com" ] ]; $result = array_merge_recursive($array1, $array2); print_r($result); ?>

Output:

Array ( [person] => Array ( [name] => Array ( [0] => Alice [1] => Bob ) [age] => 25 [email] => bob@example.com ) )

In this example, the arrays are nested under the "person" key. The "name" keys from both arrays are merged into an array, while the other keys ("age" and "email") are added to the result as normal.

Example 4: Numeric Keys in array_merge_recursive()

Numeric keys are treated differently in array_merge_recursive(). Instead of merging values into an array, numeric keys are appended, similar to array_merge().

<?php $array1 = [1, 2, 3]; $array2 = [4, 5]; $result = array_merge_recursive($array1, $array2); print_r($result); ?>

Output:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

Here, the numeric keys are re-indexed, and the values are appended to the resulting array, similar to array_merge().

Example 5: Merging Arrays with Multiple Levels

For more complex, deeply nested arrays, array_merge_recursive() works on all levels of the structure.

<?php $array1 = [ "person" => [ "name" => "Alice", "details" => [ "age" => 25, "city" => "New York" ] ] ]; $array2 = [ "person" => [ "name" => "Bob", "details" => [ "city" => "Los Angeles", "hobby" => "swimming" ] ] ]; $result = array_merge_recursive($array1, $array2); print_r($result); ?>

Output:

Array ( [person] => Array ( [name] => Array ( [0] => Alice [1] => Bob ) [details] => Array ( [age] => 25 [city] => Array ( [0] => New York [1] => Los Angeles ) [hobby] => swimming ) ) )

Here, the values of "name" and "city" from both arrays are combined into arrays, while "age" and "hobby" are merged normally.

Practical Usage:

  • array_merge_recursive() is useful for:
    • Merging multidimensional arrays where you want to keep all values, rather than overwriting them.
    • Handling configuration data or other structured data where different sources might provide overlapping keys and you need to preserve all data.

Summary:

  • array_merge_recursive() merges two or more arrays into a single array.
  • When two arrays have the same string key, their values are merged into an array, rather than overwriting the previous value.
  • Numeric keys are appended and re-indexed, like in array_merge().
  • The function works recursively, meaning it can handle nested arrays at all levels.

This function is especially useful when you want to retain all values for the same keys, making it ideal for merging configurations or multidimensional data.