PHP append one array to another (not array_push or +)

PHP append one array to another (not array_push or +)

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

Stefan Bogdanescu

Founder & Senior Architect

Concatenating Arrays in PHP: Beyond array_push and the + Operator

Developers often find themselves needing to merge two arrays while preserving their specific key structures. In standard PHP, if you define $a = ['a', 'b'] and $b = ['c', 'd'], your goal is likely to produce a single contiguous sequence: ['a', 'b', 'c', 'd']. However, the tools available for this operation carry distinct side effects that can easily lead to nested structures or data loss.

The most common trap involves using array_push(). While intuitive, this function treats its arguments as sources of elements to append sequentially. If you pass an array directly into array_push(), PHP expects individual scalar values. When you accidentally pass the entire $b array as a single argument without unpacking it first, or if you are working with multi-dimensional structures, array_push() will wrap the incoming data into a new sub-array. The result looks like ['a', 'b', ['c', 'd']], creating a nested structure rather than a flat concatenation. To flatten this manually requires additional logic to iterate over the nested array and push individual elements, which defeats the purpose of using a helper function in the first place.

Another frequent mistake is relying on the + operator. In PHP, $a + $b performs a "union" operation that is heavily dependent on keys. It appends values from $b only if they do not exist as keys in $a. If both arrays use numeric indices starting from zero, this approach often results in overwriting or skipping data entirely, rather than simply appending to the end of $a. For instance, if $a = [0 => 'x'] and $b = [0 => 'y'], $a + $b yields ['x', 'y'] because the second element uses index 1. However, if keys are sparse or string-based, the behavior shifts to merging based on key identity rather than physical position, which can lead to subtle bugs in data aggregation logic.

The robust solution for appending one array to another while strictly controlling key generation lies in using the concatenation operator (.) or a simple loop. The dot operator $a . $b is specifically designed to append values from the second operand to the first without altering the keys of either array. If both arrays are numeric, this operation creates a new contiguous numeric sequence. This method is concise and readable, but it creates a new array instance in memory rather than modifying $a in place.

If you prefer an in-place modification or need to handle associative arrays where key preservation is critical, a foreach loop offers granular control. Iterating through the source array with foreach ($b as $var) and assigning $a[] = $var guarantees that new elements are appended at the end of $a with auto-incrementing integer keys, regardless of what $b contains. This approach effectively simulates the behavior you described: taking every element from $b and placing it sequentially after the last element of $a.

While modern PHP versions offer the array_merge() function to combine arrays, it is worth noting that this function re-indexes numeric keys starting from zero. If your application relies on preserving specific indices for caching or sorting logic, array_merge() will destroy that data structure, whereas the concatenation operator or the explicit loop approach maintains the integrity of the resulting index sequence.

For those building scalable applications using the Laravel framework, understanding these low-level array behaviors is crucial before utilizing collection methods like concat(). While Laravel's collections provide a fluent interface for merging arrays, being comfortable with raw PHP array operations ensures you can handle edge cases where collection overhead is unnecessary or when debugging performance bottlenecks in tight loops.

Ultimately, the choice between using the dot operator and a loop depends on your specific needs regarding memory efficiency and mutability. The dot operator is generally preferred for readability in expression-heavy code, while the loop provides explicit control over the assignment process, ensuring that keys are handled exactly as intended without implicit re-indexing surprises.

Our network