array_merge_recursive
Merges arrays, combining nested arrays that share a key.
Syntax
array_merge_recursive(array ...$arrays): arrayParameters
| Parameter | Type | Description |
|---|---|---|
| ...$arrays | array | The arrays to merge. |
Return value
arrayA new array.
Examples
Nested lists are combined:
<?php
$a = ['tags' => ['php'], 'title' => 'Post'];
$b = ['tags' => ['sql'], 'title' => 'Draft'];
echo json_encode(array_merge_recursive($a, $b));Output:
{"tags":["php","sql"],"title":["Post","Draft"]}Notes
Equal scalar values are not replaced but gathered into an array — like title in the example. To replace instead, use array_replace_recursive().
See also
- array_merge — Merges one or more arrays into one.
- array_replace — Replaces the values of an array with values under the same keys from other arrays.
- array_combine — Creates an array from two lists: keys and values.
In the tutorial
- Array functions — Processing arrays in PHP without loops — array_map, array_filter, array_reduce, searching and slicing.