Array destructuring in PHP unpacks values from an array into variables in one statement, using [$a, $b] = $array or ['key' => $x] = $array. The spread operator ... does the reverse: it expands an array into function arguments or into another array. Together, php array destructuring spread syntax replaces many list() calls, array_merge() calls and index lookups with shorter code.
Destructuring has existed since PHP 7.1 and spread inside array literals since 7.4, with string-key spread added in 8.1. The examples run on PHP 8.3 and later.
foreach and function returnsSquare brackets on the left side of an assignment pull values out by position.
This is the modern form of list(), which still works but is rarely needed. Skip values you do not need by leaving the slot empty.
If the array has fewer elements than variables, PHP emits a warning for the undefined key and the variable is null, so check lengths for user input.
Destructuring makes a swap a single line with no temporary variable.
For associative arrays, name the keys you want.
The key order does not matter, and you only list the keys you need. A missing key gives a warning and null.
Patterns can be nested to reach into multi-level data.
foreach and function returnsDestructuring is most useful inside loops over rows.
A function that needs to return several values can return an array and the caller destructures it.
For more than two or three values, return an object or a typed class instead, because positions are easy to confuse.
Inside an array literal, ... expands another array or any Traversable.
Integer keys are renumbered, and with string keys later entries overwrite earlier ones, exactly like array_merge(). String-key spread requires PHP 8.1 or later.
Spread unpacks an array into positional arguments, and with string keys into named arguments (PHP 8.1 and later).
The same ... in a parameter list collects extra arguments into an array.
Here the dots mean two opposite things: in the definition they pack arguments, and at the call site they unpack an array.
| Pitfall | What happens | Fix |
|---|---|---|
| Missing index or key | Warning and null value | Check with isset() or provide a default with ?? |
| Spreading a non-iterable | Error is thrown | Make sure the value is an array or Traversable |
| Duplicate string keys in spread | Later value wins | Order the arrays deliberately |
| Spread on PHP before 8.1 with string keys | Fatal error | Use array_merge() |
list() deprecated?No, it still works, but the short [...] syntax does the same thing and is the common style.
Not directly. Assign first, then use the null coalescing operator, for example $name = $user['name'] ?? 'Guest';.
array_merge()?They behave the same for lists and string keys. Spread works inline with literal values and accepts any Traversable, while array_merge() needs arrays.
Not with this syntax. Destructuring works on arrays and objects that implement ArrayAccess. For other objects, read the properties directly.
Destructuring unpacks arrays by position or by key into variables, in assignments, foreach loops and function returns. The spread operator expands arrays into other arrays and into function arguments, and a variadic parameter does the reverse by collecting arguments. Prefer these forms for short, readable code, and guard against missing keys when the data comes from outside your program.