array_merge()

array_merge() — Merge one or more arrays.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

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.

Signature

array_merge(array ...$arrays): array

Parameters

ParameterTypeWhat it takes
...$arraysarrayVariable list of arrays to merge.

Return value

Returns the resulting array. If called without any arguments, returns an empty array.

Example

A runnable snippet:

$array1 = ["color" => "red", 2, 4]; $array2 = ["a", "b", "color" => "green", "shape" => "trapezoid", 4]; $result = array_merge($array1, $array2); print_r($result);

Output:

Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 )

Changelog

VersionChange
7.4.0This function can now be called without any parameter. Formerly, at least one parameter has been required.

See also

array_merge_recursive(), array_replace(), array_combine()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page array_merge() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.