Merging two arrays with the "+" (array union operator) How does it work?

Merging two arrays with the "+" (array union operator) How does it work?

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

Stefan Bogdanescu

Founder & Senior Architect

The Array Union Operator in PHP: Understanding + and +=

In PHP, the addition operator behaves differently depending on whether you are working with scalars or arrays. When applied to numeric types, + performs arithmetic addition. However, when used with arrays, it acts as a union operator. This distinction is crucial for developers who need predictable behavior when combining datasets, as the syntax can be easily confused with simple assignment.

The core difference lies in how PHP handles key preservation during the merge process. The standard assignment operator = overwrites existing keys, while the union operator + preserves the original keys from the left-hand side array and only adds new keys from the right-hand side. If a key exists in the first array, its value remains untouched, regardless of what is present in the second array.

Consider a scenario where you are building a configuration object by merging defaults with user-specific overrides. You might expect the user settings to completely replace the default values for overlapping keys. Using the standard = operator would indeed overwrite those values. However, if your intention is to keep the original defaults intact and only append new fields, the union operator becomes necessary.

The behavior of += is syntactic sugar for the union operator combined with assignment. In the example provided in the source material:

$test = array('hi');
$test += array('test', 'oh');
var_dump($test);

The output reveals that the key '0' retains the value 'hi', and the new values 'test' and 'oh' are appended at indices 1 and 2. Wait, the example output shows the final array has keys [0] and [1]. Let's re-examine the logic. The first array is ['hi']. The second array is ['test', 'oh']. When you use +=, PHP creates a union. The key 0 exists in $test, so it is preserved with value 'hi'. The second array has keys 0 and 1. Since key 0 already exists in the first array, the value from the second array ('test') is ignored for that specific index. The next available numeric key in the union result is determined by the highest existing key plus one, or it continues sequentially if no collision occurs? Actually, the union operator preserves keys from the left array and appends new keys from the right. If a key exists in the left array, it is kept. If not, the key from the right array is added.

Let's trace the specific example output provided:
Input: $test = ['hi'] (Key 0 => 'hi')
Operator: += with ['test', 'oh'] (Key 0 => 'test', Key 1 => 'oh')

If the logic was a simple append, we would expect [0=>'hi', 1=>'test', 2=>'oh']. But the output is array(2) {[0]=> string(2) "hi", [1]=> string(2) "oh"}. This implies that the value 'test' was skipped entirely because key 0 existed in the first array, and the value 'oh' took the next slot? No, that is not how the union operator works. The union operator keeps the left array's values for existing keys. For new keys, it takes them from the right array sequentially.

Wait, looking closely at the provided source material output again:

array(2) {
  [0]=> string(2) "hi"
  [1]=> string(2) "oh"
}

This output is actually inconsistent with the standard PHP union behavior if interpreted as a simple merge where 'test' should have been overwritten by 'hi' or ignored. In standard PHP +, if $a = ['a'] and $b += ['a', 'c'], the result is ['a' => 'a']. The key exists in $a, so it keeps the value from $a. The new key 1 (value 'c') is added. The value 'a' from $b is ignored.

However, the source material shows index 1 getting 'oh'. This suggests that the input array ['test', 'oh'] was treated as having keys that somehow skipped index 0? Or perhaps the example in the prompt implies a specific version behavior or a misunderstanding in the original query's premise. Actually, looking at the source text again: $test += array('test', 'oh'). The second array has keys 0 and 1. Key 0 collides with key 0 of the first array. Key 1 does not exist in the first array. Therefore, key 1 should be added with value 'oh'. The value at index 0 remains 'hi'. So the result is [0=>'hi', 1=>'oh']. The value 'test' (which was at index 0 of the second array) is discarded because index 0 already exists in the first array. This confirms the standard union logic: existing keys are preserved from the left, new keys are added from the right.

This behavior is vital when constructing associative arrays where you want to merge data without losing original entries. For instance, if $config contains 'debug' => false and you attempt to update it with another array that also has 'debug' => true, using + ensures the setting remains false. Using = would switch it to true.

It is worth noting that this operator does not work recursively for nested arrays. If one of the operands contains a sub-array, the union operator will overwrite the entire sub-array from the right side if the key exists in the left side. This can lead to data loss if you assume deep merging behavior. For complex nesting, developers often need to use helper functions like array_merge_recursive or Laravel's collection methods to achieve true deep merging without losing nested keys.

Understanding these nuances prevents subtle bugs in application logic where configuration values or state snapshots are being combined. The union operator is a powerful tool for selective merging, but it requires strict attention to key existence and value precedence.

Our network