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

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

php PHP 8.1+ Beginner

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 21
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 3 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 6 Unknown AI 4 Perplexity 3 ChatGPT 2 Google 1 Ahrefs 1
crawler 14 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