To merge arrays in PHP, use array_merge() or the spread operator for lists, the + operator to keep the left array's values for duplicate keys, and array_merge_recursive() or array_replace_recursive() for nested data. The key difference is how each handles duplicate keys: array_merge() overwrites string keys and renumbers integer keys, while + never overwrites.
This guide explains php array_merge, its relatives and the traps around numeric keys. Examples target PHP 8.3 and later.
array_merge()+ union operator...array_replace()array_combine()array_merge()array_merge() appends the elements of the later arrays to the first and returns a new array.
Integer keys are renumbered from zero, so no values are lost. Since PHP 7.4 you can call it with no arguments and it returns an empty array, which is useful when you spread a possibly empty list of arrays.
With string keys, a later value overwrites an earlier one.
This makes array_merge($defaults, $options) the standard way to apply default settings, since the second array wins.
+ union operatorThe + operator also combines arrays, but the left array wins for keys that exist in both, and integer keys are not renumbered.
In the second line, keys 0 and 1 already exist on the left, so only the third element of the right array is added. Use + to fill in missing keys and array_merge() to append lists. Reversing the operands, $custom + $defaults, gives the same result as array_merge($defaults, $custom) for string keys.
...Since PHP 7.4 the spread operator can unpack arrays inside an array literal. From PHP 8.1 it also works with string keys.
Spreading has the same rules as array_merge(): integer keys are renumbered and later string keys overwrite earlier ones. It is handy when you mix literal values with arrays in one expression.
array_replace()array_replace() overwrites values by key and keeps integer keys as they are, instead of appending.
array_merge() is shallow: a nested array under the same key is replaced entirely. Two recursive functions behave differently.
array_merge_recursive() turns duplicate scalar values into arrays, which is rarely what you want for configuration. For settings, array_replace_recursive() is usually the right choice.
array_combine()To build one array from a list of keys and a list of values, use array_combine(). Both lists must have the same length, or PHP 8 throws a ValueError.
| Goal | Use |
|---|---|
| Append lists | array_merge() or [...$a, ...$b] |
| Defaults overridden by options | array_merge($defaults, $options) |
| Keep left values, add missing keys | $a + $b |
| Overwrite by integer key | array_replace() |
| Deep settings merge | array_replace_recursive() |
| Keys from one list, values from another | array_combine() |
array_merge() and +?array_merge() renumbers integer keys and lets later string keys overwrite. + keeps the left array's value on a key clash and does not renumber.
+?Both arrays had the same integer keys, so the right side was ignored. Use array_merge() for lists.
array_merge() change the original arrays?No. It returns a new array and leaves the inputs unchanged.
Use the spread operator on an array of arrays: array_merge(...$listOfArrays).
Use array_merge() or spread to concatenate lists and to apply options over defaults, + to add only missing keys, and array_replace() to overwrite by key. For nested configuration, pick array_replace_recursive() rather than array_merge_recursive(). Remember that integer keys are renumbered by merge and spread but kept by + and array_replace().