W3docs

PHP Array Merge: An In-Depth Guide

The PHP array_merge() function is a powerful tool for combining arrays into a single array. This function takes two or more arrays as arguments and merges them

The PHP array_merge() function combines two or more arrays into a single array. It takes one or more arrays as arguments and returns a new array; the original arrays are left unchanged. This guide covers how array_merge() handles string keys, numeric keys, and nested arrays, the gotchas to watch for, and how it differs from the + union operator.

The two rules that explain almost every result are:

  • String keys that appear in more than one array are overwritten — the value from the later array wins.
  • Numeric keys are never overwritten. Each value is appended and the keys are reindexed sequentially starting from 0.

Note: As of PHP 8.0, passing a non-array argument to array_merge() throws a TypeError. (In PHP 7 it raised a warning and returned null.) Calling array_merge() with no arguments returns an empty array.

Basic Usage of array_merge()

The basic usage of the array_merge() function is straightforward. Simply pass in two or more arrays as arguments, and the function will return a new array that is the result of merging the arrays. Here is an example:

PHP array_merge function example

php— editable, runs on the server

Output:

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

Notice two things in the output:

  • The string key color exists in both arrays, so $array2's value (green) overwrote $array1's value (red).
  • The numeric keys were not preserved. The original 2 and 4 from $array1 and the 4 from $array2 were all kept (nothing was overwritten) and renumbered 0, 1, 4.

Reindexing of Numeric Keys

Because numeric keys are renumbered, you cannot rely on the original integer keys surviving a merge. Even calling array_merge() on a single array is a common, fast way to reindex it:

<?php

$nums = [10 => "a", 25 => "b", 7 => "c"];
$result = array_merge($nums);
print_r($result);

?>

Output:

Array
(
    [0] => a
    [1] => b
    [2] => c
)

If you need to keep specific integer keys, use the + union operator or array_replace() instead.

Merging Multidimensional Arrays

The array_merge() function can also be used to merge multidimensional arrays. To merge multidimensional arrays, simply pass in each array as an argument to the function. Here is an example:

PHP To merge multidimensional arrays by using array_merge_recursive

php— editable, runs on the server

Output:

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

Note that array_merge() does not merge nested arrays. When two arrays share a string key whose value is itself an array, the later array's nested value replaces the earlier one wholesale — here ["favorite" => "green"] simply overwrote ["favorite" => "red"]. To merge nested arrays recursively instead of overwriting them, use array_merge_recursive().

array_merge() vs the Union Operator

PHP also offers the + union operator for combining arrays, but it behaves very differently from array_merge(). The union operator keeps the value from the left operand whenever keys collide, and it never reindexes numeric keys.

<?php

$array1 = ["a", "b", "c"];
$array2 = ["x", "y", "z", "w"];

print_r(array_merge($array1, $array2)); // appends + reindexes
print_r($array1 + $array2);             // keeps left, no reindex

?>

Output:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => x
    [4] => y
    [5] => z
    [6] => w
)
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => w
)

With array_merge(), every element is kept and renumbered. With +, indexes 02 already existed in $array1 so they win, and only index 3 (w) is pulled in from $array2. Reach for + when you want to fill in missing keys (such as merging user options over defaults) while preserving existing values and integer keys; reach for array_merge() when you want a flat, sequentially indexed list. See PHP Operators for more.

Merging an Indexed Array with an Associative Array

When merging an indexed array with an associative array, the values from both arrays are combined. Here is an example:

PHP Example of merging an indexed array with an associative array by array_merge function

php— editable, runs on the server

Output:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [a] => apple
    [b] => banana
    [c] => cherry
)

As you can see, the values from both arrays are combined. Since the keys are different types (numeric vs. string), no overwriting occurs. This is an important detail to keep in mind when merging arrays with different key types.

Merging an Associative Array with an Indexed Array

The result of merging an associative array with an indexed array is similar. The values are combined, and the keys are preserved according to their type. Here is an example:

PHP merging an associative array with an indexed array by using array_merge function

php— editable, runs on the server

Output:

Array
(
    [a] => apple
    [b] => banana
    [c] => cherry
    [0] => a
    [1] => b
    [2] => c
)

As shown, the values combine without conflict. String keys from $array1 are preserved, while numeric keys from $array2 are reindexed starting from 0.

Performance Considerations

The array_merge() function has a linear time complexity, meaning that the time it takes to merge arrays increases linearly with the size of the arrays. For this reason, it's important to keep the number of arrays being merged to a minimum, and to avoid using the function on extremely large arrays.

Conclusion

The array_merge() function is a versatile tool for combining arrays in PHP. Remember the two core rules: string keys are overwritten by later arrays, while numeric keys are appended and reindexed from 0. When those defaults don't fit your case, choose a related function instead:

Practice

Practice
What is true about the array_merge function in PHP?
What is true about the array_merge function in PHP?
Was this page helpful?