← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Spread Operator in Arrays [...$a, ...$b]

PHP PHP 8.1+ Beginner
debt(d5/e1/b1/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches' (d5), Rector can detect and transform array_merge() to spread syntax, but the key-reindexing surprise is silent until tests catch it.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1), quick_fix is a direct replacement of array_merge($a, $b) with [...$a, ...$b].

b1 Burden Structural debt — long-term weight of choosing wrong

Closest to 'minimal commitment' (b1), this is a localised syntactic choice per array literal with no architectural reach.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap most devs eventually learn' (t5), misconception states integer keys are re-indexed (not preserved) — a documented gotcha matching array_merge semantics that surprises devs expecting key preservation.

About DEBT scoring →

TL;DR

PHP 8.1+ allows the spread operator ... inside array literals to merge arrays — cleaner than array_merge() for combining arrays inline.

Explanation

PHP 7.4 added spread in function calls. PHP 8.1 added spread in array expressions: [...$a, ...$b] creates a new merged array. Key behavior: integer keys are re-indexed (like array_merge), string keys from later arrays overwrite earlier ones (like array_merge). PHP 8.1 also allows string-keyed spreads. Spread preserves no reference — it creates copies. For associative merging with last-write-wins semantics, [...$defaults, ...$overrides] is idiomatic.

Common Misconception

Spread in arrays preserves original integer keys — like array_merge(), integer keys are re-indexed from 0.

Why It Matters

Array spreading with ... removes the need for array_merge() in most cases, which has subtle behavioural differences with numeric keys — array_merge() reindexes, spread preserves. In config merging, test fixture building, and variadic function calls, spread syntax produces cleaner, more predictable code. It also enables named arguments spread from associative arrays in PHP 8.1+, which is useful for forwarding options through a call chain.

Common Mistakes

  • Expecting integer keys to be preserved — they're re-indexed.
  • Using spread with string keys before PHP 8.1 — fatal error in PHP 7.4 spread.
  • Not knowing that [...$a, ...$b] and array_merge($a, $b) behave identically for simple cases.

Code Examples

✗ Vulnerable
$merged = array_merge($defaults, $userConfig, ['debug' => false]);
// Works but verbose
✓ Fixed
// PHP 8.1+ idiomatic:
$config = [...$defaults, ...$userConfig, 'debug' => false];

// Cloning with overrides:
$updated = [...$original, 'status' => 'active', 'updated_at' => now()];

// Conditional spread:
$params = [
    'limit' => 20,
    ...($search ? ['q' => $search] : []),
];

Added 22 Mar 2026
Edited 23 Mar 2026
Views 37
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 2 pings M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 7 ChatGPT 4 Unknown AI 4 Scrapy 4 Perplexity 3 Ahrefs 3 Claude 2 SEMrush 2 Google 1 PetalBot 1
crawler 26 crawler_json 2 pre-tracking 3
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Replace array_merge($a, $b) with [...$a, ...$b] for clearer array combination. Use [...$defaults, ...$overrides] for config merging patterns.
📦 Applies To
PHP 8.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
array_merge\(
Auto-detectable: ✓ Yes rector
🤖 AI Agent
Confidence: Low False Positives: Medium ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant