PHP Merged the Records. Where Did the IDs Go?

PHP array_merge can keep every value while renumbering ID keys. Choose list append, map replacement or union, then verify the JSON shape.

Share
Orange record cards move from separate trays into evenly spaced array positions.

The records survived the merge. Their IDs didn’t. If a PHP array is keyed by numeric identifiers, array_merge() can turn those keys into 0, 1, 2…. It appends values under numeric keys and renumbers them; it doesn’t treat those keys as record identities. Before patching the lookup that now fails, decide whether you’re joining a list, replacing entries in a map, or filling missing entries. Those need different operations.

This is an AI-prepared, documentation-based explainer, not a personally reproduced incident. The examples are illustrative and were not executed for this article. See the editorial policy.

The values look right until you look them up

Imagine two batches of records indexed by product ID. The application merges them, checks that all the names are present, and sends the result to a worker. Later the worker asks for product 42 and gets nothing. The data is sitting there under a shiny new key. Apparently it entered witness protection during the merge.

This illustrative PHP fragment isolates the boundary:

$first  = [42 => 'original', 105 => 'keep'];
$second = [42 => 'updated', 208 => 'new'];

$merged = array_merge($first, $second);
// Expected:
// [0 => 'original', 1 => 'keep', 2 => 'updated', 3 => 'new']

There are four values, including both versions of product 42. The key no longer tells you which record each value belongs to. That behavior follows the array_merge contract: later string keys overwrite earlier string keys, while numeric keys are renumbered in the result. A test using 'name' and 'status' as keys can therefore pass while the production ID-keyed input behaves differently.

Check the key type at the first changed boundary

Inspect the keys immediately before and after the operation, rather than dumping entire records. In a disposable test, this illustrative diagnostic reports their types without printing record contents:

foreach ($first as $key => $_) {
    echo gettype($key), ':', $key, PHP_EOL;
}
var_export(array_keys(array_merge($first, $second)));

Quoting the ID doesn’t necessarily fix this. PHP’s array key conversion rules convert a valid decimal-integer string such as "42" to integer key 42. A string such as "042" stays a string. That’s a reason to test the real input shape, not a reason to pad IDs until the bug goes away. Altering an identifier’s representation can introduce a different mismatch at the next boundary.

If the keys already changed before the merge, trace the input construction or an earlier reindexing operation. If they change exactly at array_merge(), the function is doing what it promises. The mistaken assumption was that “merge” meant “combine these keyed records using my collision policy.” PHP would like that policy in writing.

Pick the collision rule explicitly

What the input meansOperationResult for key 42 above
Append list elements; positions may changearray_merge($first, $second)No key 42; both values remain under new positions
Map entries; later batch winsarray_replace($first, $second)'updated'
Map entries; existing left entry wins$first + $second'original'

array_replace() replaces values by key, preserving numeric keys rather than renumbering them. Keys absent from the first array are added. For the example, the expected result is [42 => 'updated', 105 => 'keep', 208 => 'new']. The array union operator keeps the left-hand value when the same key exists on both sides. That gives [42 => 'original', 105 => 'keep', 208 => 'new'].

Neither choice resolves duplicate records by inspecting an id field inside each value. If the inputs are lists of records, first define how IDs become keys and what should happen when the same ID appears twice. Reject a collision if silently keeping either version would be wrong. And these operations are shallow: replacing a keyed nested record can replace that entire value. Don’t switch to a recursive operation without specifying how nested fields should combine.

Verify the next boundary too

If the result becomes JSON, inspect both keys and wire shape. The JSON encoding rules distinguish a sequential zero-based array from an array with other keys. Reindexing can change an object-shaped map into a JSON list. Conversely, preserving sparse numeric keys can produce a JSON object when a client expects a list. Fixing the PHP lookup is only half the job if a consumer relies on that distinction.

Write regression checks for a collision, a new key, an empty batch, integer-like string keys, and the expected serialized shape. Assert the value under the original identifier, not just the total count or the presence of a name. If the consumer wants a list, convert deliberately after all map-based work is finished and keep record IDs inside the records.

Debugly’s aliased-array investigation covers a different way collections can surprise you: shared mutation. Here there’s no mysterious mutation to chase. The merge returned a new array using list rules for numeric keys. Give the operation the collision rule your records actually need, and the IDs can stay out of witness protection.