What's the difference between array_merge and array + array?

What's the difference between array_merge and array + array?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Mechanics Behind array_merge and + in PHP

In everyday PHP development, choosing between array_merge() and the + operator often feels like splitting hairs. Both functions produce an array containing elements from both operands. However, beneath that superficial similarity lies a critical distinction regarding how keys are handled during the merging process. Understanding this difference is essential when dealing with associative arrays or when preserving specific configuration structures.

The Fate of Numeric Keys

When you use array_merge(), PHP performs a deep merge for numeric indices. It iterates through both arrays, collecting all values into a new array while re-indexing the keys sequentially starting from zero. This behavior is predictable but destructive to the original key structure if duplicate numeric keys exist.

$array1 = ['a' => 1, 0 => 'first', 1 => 'second'];
$array2 = [0 => 'third', 2 => 'fourth'];

$merged = array_merge($array1, $array2);
// Result:
// Array ( 
//     [a] => 1 
//     [0] => first 
//     [1] => second 
//     [2] => fourth 
// )

In this example, the key 0 from $array1 is kept as 0. The key 0 from $array2 overwrites it because they share the same numeric identifier. Subsequent keys are re-indexed to avoid collisions, shifting values up if necessary. This makes array_merge ideal for combining lists of items where you don't care about maintaining specific original indices, or when you want to concatenate two sequential lists into one continuous stream.

The + Operator and Key Preservation

The + operator takes a different approach regarding numeric keys. When PHP encounters duplicate keys in the left-hand operand versus the right-hand operand during a + operation, it does not re-index. Instead, it preserves the key from the left operand and simply ignores the value for that key in the right operand.

$array1 = ['a' => 1, 0 => 'first', 1 => 'second'];
$array2 = [0 => 'third', 2 => 'fourth'];

$merged = $array1 + $array2;
// Result:
// Array ( 
//     [a] => 1 
//     [0] => first 
//     [1] => second 
//     [2] => fourth 
// )

Notice that the value 'third' from $array2 was discarded because key 0 already existed in $array1. The resulting array looks identical to the array_merge() example, but the mechanism is strictly additive: left values take precedence for duplicate keys. This behavior extends to associative keys as well; if both arrays contain a key 'name', the value from $array2 will be ignored entirely.

Practical Trade-offs and Pitfalls

The choice between these two methods often depends on whether you need to preserve original indices or simply combine values. If you are building a configuration file where specific settings might be overridden, array_merge() is dangerous if it causes unintended index shifting or if you rely on the order of insertion for logic later in your application.

Conversely, the + operator is excellent for "partial updates." Imagine you have a base set of database columns and you want to add new fields without disturbing the existing column order or accidentally overwriting a critical value that was explicitly defined earlier. Using array_merge() here might result in unexpected behavior if your code assumes specific numeric keys exist at certain positions.

A common pitfall occurs when working with sparse arrays (arrays with missing intermediate numeric indices). array_merge() will fill those gaps by re-indexing, whereas + preserves the sparsity and the original keys intact. If your application logic depends on checking isset($arr[5]) expecting a specific gap, array_merge() could break that assumption by compacting the array.

When to Use Which

If you are combining simple lists of data where key identity doesn't matter, array_merge() provides the safety of guaranteed sequential indexing. However, if you are merging configuration arrays or need to ensure that a specific key from the first array remains untouched regardless of what the second array offers, the + operator is the robust choice.

For developers working within the Laravel ecosystem, understanding this distinction becomes even more relevant when merging request attributes or binding model attributes. While Laravel provides helper methods for many complex merging scenarios, knowing how these native PHP operators behave ensures you don't introduce subtle bugs when manually combining data structures before passing them to Eloquent models. For deeper dives into array manipulation patterns in modern PHP applications, resources like the LaravelCompany blog often highlight best practices for handling data aggregation efficiently without sacrificing performance or readability.

Our network