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:
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()
.
Output:
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.
Output:
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.
Output:
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()
.
Output:
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.
Output:
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.