Codectionary / Developer documentation / PHP

Combine Arrays with array_merge

Append list items and combine named entries while understanding numeric reindexing and string-key replacement.

What it is and why to use it

Combining arrays is common when joining task lists or applying configuration overrides. array_merge() does both, but its behaviour depends on the key type. Integer-keyed entries are appended and renumbered. If string keys collide, the value in the later array wins. Understanding that distinction prevents accidentally treating identifiers like list positions.

How it works

Arguments are processed from left to right into a new array. Repeated values are retained; this is not a deduplication operation. Named options use replacement semantics, while integer keys lose their original numbering. The array union operator + is different: it preserves keys and keeps the left-hand value on any key collision. Choose based on the data you are combining.

Common mistakes and limitations

Do not use array_merge when numeric keys are record IDs that must survive. It does not recursively merge nested arrays, and + does not append colliding list positions. String keys containing valid decimal integers can become integer keys when the array is created, so inspect your actual keys.

Run the examples locally

Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.

The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.

These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.

Check your interpreter, lint and run one saved example

php -v
php -l example.php
php example.php

Continue learning

Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.

Syntax

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

Examples

Append two short reading lists

Observe that old numeric positions are replaced with sequential positions.

<?php
declare(strict_types=1);

$morning = [5 => 'Arrays'];
$evening = [9 => 'Functions', 12 => 'Arrays'];
$reading = array_merge($morning, $evening);
echo json_encode($reading, JSON_THROW_ON_ERROR), PHP_EOL;
  1. The input keys 5, 9 and 12 are integers, so the values are appended in argument order.
  2. The result receives keys 0, 1 and 2, allowing JSON to represent it as an array.
  3. Arrays appears twice because merge retains duplicate values. Use a separate deduplication step if repetition is unwanted.
  4. Expected output: ["Arrays","Functions","Arrays"]

Compare overrides with array union

Apply the same options using two operations that intentionally give different answers.

<?php
declare(strict_types=1);

$defaults = ['theme' => 'light', 'pageSize' => 10];
$overrides = ['theme' => 'dark'];
$merged = array_merge($defaults, $overrides);
$union = $defaults + $overrides;
echo $merged['theme'], PHP_EOL;
echo $union['theme'], PHP_EOL;
  1. theme is a string key in both arrays.
  2. array_merge lets the later dark value replace light, while pageSize remains available.
  3. The + operator keeps the existing left-hand key, so its theme remains light. This is useful when adding missing defaults rather than overriding supplied values.
  4. Expected output: dark light

Best practices

  • Use merge for lists and explicit named overrides.
  • Use replacement or union when preserving numeric keys matters.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading