array_merge() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way. For associative arrays, duplicate entries will be merged into the last one. See example three for details.
Example 2. Simple array_merge() example
Don't forget that numeric keys will be renumbered!
If you want to completely preserve the arrays and just want to append them to each other, use the + operator:
The numeric key will be preserved and thus the association remains.
|
Example 3. array_merge() example
The above example will output:
|
Note: Shared keys will be overwritten on a first-come first-served basis.
See also array_merge_recursive(), array_combine() and array operators.