Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

array_merge

Merges one or more arrays into one.

Available since: PHP 4

Syntax

array_merge(array ...$arrays): array

Parameters

ParameterTypeDescription
...$arraysarrayThe 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, 4

When 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

Related recipes

In the tutorial

  • Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.

Thanks for reading!

Donate to the site — we work hard for you.