array_merge
Merges one or more arrays into one.
Available since: PHP 4
Syntax
array_merge(array ...$arrays): arrayParameters
| Parameter | Type | Description |
|---|---|---|
| ...$arrays | array | The arrays to merge. Any number can be passed. |
Return value
arrayA new array with the elements of all the given arrays.
Examples
Lists are appended and renumbered:
<?php
echo implode(', ', array_merge([1, 2], [3, 4]));Output:
1, 2, 3, 4When string keys collide, the later array wins:
<?php
$defaults = ['color' => 'red', 'size' => 'M'];
$options = ['size' => 'L'];
print_r(array_merge($defaults, $options));Output:
Array
(
[color] => red
[size] => L
)Notes
The source arrays are not modified. Calling it with no arguments returns an empty array.
See also
- array_unique — Removes duplicate values from an array.
- array_slice — Returns part of an array.
Related recipes
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.