PHP array_merge() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_merge() joins one or more arrays into a single new array.

Numeric keys are renumbered and items are appended. For string keys, a later value replaces an earlier one.

Syntax

Call array_merge() with the values you need. Start with this line.

PHP
$all = array_merge($first, $second);
CallWhat it does
array_merge([1, 2], [3, 4])Returns [1, 2, 3, 4].
array_merge(["a" => 1], ["a" => 2])Returns ["a" => 2], because the later string key wins.
array_merge(...$groups)Merges every array stored in $groups.

The + operator also combines arrays, but it keeps the first value for duplicate keys, which is the opposite of array_merge().

Library declarations
PHP
array_merge(array ...$arrays): array

Parameters

ParameterDescription
arraysOptional. One or more arrays to merge. Calling it with no arguments returns an empty array.

Return Value

Return ValueDescription
arrayA new array containing the items of all the given arrays.

PHP Examples

Example 1 — Join Two Lists

Combine two simple arrays into one.

PHP
<?php
$first = [1, 2];
$second = [3, 4];

$all = array_merge($first, $second);

echo implode(", ", $all), PHP_EOL;

How It Works

  1. Create two arrays. One holds 1 and 2, the other 3 and 4.
  2. Call array_merge(). The second array is added after the first.
  3. Print the result. All four numbers are in order.
  4. The originals are unchanged. A new array is returned.

Example 2 — String Keys vs Numeric Keys

String keys are overwritten, but numeric keys are appended and renumbered.

PHP
<?php
$a = array_merge(["a" => 1, "b" => 2], ["b" => 3, "c" => 4]);
echo json_encode($a), PHP_EOL;

$b = array_merge([5 => "x"], [5 => "y"]);
echo json_encode($b), PHP_EOL;

How It Works

  1. Merge string keys. Key b exists in both, so the later value 3 wins.
  2. Check the order. Keys stay in the order they first appeared.
  3. Merge numeric keys. Both arrays use key 5, but they are appended instead of replaced.
  4. See the renumbering. The result is a list with keys 0 and 1.

Example 3 — No Arguments and the Spread Operator

array_merge() with no arguments is allowed, and the ... operator merges arrays stored in another array.

PHP
<?php
echo json_encode(array_merge()), PHP_EOL;

$groups = [[1, 2], [3], [4, 5]];
echo implode(", ", array_merge(...$groups)), PHP_EOL;

How It Works

  1. Call it with nothing. Since PHP 7.4 this returns an empty array.
  2. Create an array of arrays. Each group has a few numbers.
  3. Use the spread operator. ...$groups passes each inner array as a separate argument.
  4. Print the flat list. All the numbers are merged into one.

Frequently Asked Questions

It combines one or more arrays into a new array. Numeric keys are renumbered, and later string keys overwrite earlier ones.
With duplicate keys, array_merge() keeps the later value, and + keeps the first. array_merge() also renumbers numeric keys.
No. It returns a new array and leaves the originals as they were.
Use array_merge_recursive() or array_replace_recursive(), depending on how you want matching keys to behave.
Pass them as arguments or use the spread operator, like array_merge(...$arrays).

Next: array_merge_recursive()

array_merge_recursive() merges arrays and, when the same string key appears twice, combine.

array_merge_recursive() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful