Array Destructuring & Spread
Destructuring lets you pull multiple values out of an array into separate variables in a single statement, instead of indexing into the array field by field. The spread operator does roughly the opposite job: it expands an array's elements back out, whether into a new array literal or into a function's argument list. Together they remove a lot of the boilerplate around building and unpacking arrays.
list() and the short [$a, $b] syntax
The original way to destructure in PHP is the list() construct. Since PHP 7.1, the short array syntax [$a, $b] = $arr does the same thing and is now the more common style, mirroring how you would already write an array literal.
list() versus the short syntax
<?php
$coordinates = [10, 20];
list($x, $y) = $coordinates; // classic form
echo "{$x}, {$y}\n";
[$x, $y] = $coordinates; // short form, same result
echo "{$x}, {$y}\n";10, 20 10, 20
Skipping elements
You can leave a gap in the pattern to skip an element you do not need, by leaving that position empty between the commas.
Skipping the middle value
<?php
$row = ["Priya", 34, "Toronto"];
[$name, , $city] = $row; // skip the age
echo "{$name} lives in {$city}"; // Priya lives in TorontoPriya lives in Toronto
Keyed destructuring
When the source array is associative, you can destructure by key instead of by position - this is often clearer than positional destructuring because it does not depend on the order fields appear in, only on their names.
Destructuring by key
<?php
$user = ["id" => 7, "name" => "Femi", "role" => "editor"];
["name" => $name, "role" => $role] = $user;
echo "{$name} is a(n) {$role}"; // Femi is a(n) editorFemi is a(n) editor
Keyed destructuring also works naturally in a foreach loop when each element of the outer array is itself an associative array - a very common shape for rows pulled from a database.
Destructuring inside a foreach
<?php
$users = [
["name" => "Femi", "role" => "editor"],
["name" => "Ravi", "role" => "admin"],
];
foreach ($users as ["name" => $name, "role" => $role]) {
echo "{$name}: {$role}\n";
}Femi: editor Ravi: admin
The spread operator in array literals
The ... spread operator expands the elements of one array directly into a new array literal. It is a concise alternative to array_merge() for combining indexed arrays, and unlike array_merge(), it reads left-to-right in the exact shape of the resulting array.
Spreading indexed arrays together
<?php $fruits = ["apple", "banana"]; $moreFruits = ["cherry", "date"]; $all = [...$fruits, ...$moreFruits, "elderberry"]; print_r($all);
Array
(
[0] => apple
[1] => banana
[2] => cherry
[3] => date
[4] => elderberry
)Spreading with string keys (PHP 8.1+)
Early versions of the spread operator only worked cleanly with integer-keyed arrays. As of PHP 8.1, spreading an array with string keys is fully supported, and behaves like array_merge() for those keys - a later spread with the same key overwrites an earlier one.
Spreading associative arrays
<?php $defaults = ["theme" => "light", "retries" => 3]; $overrides = ["theme" => "dark"]; $settings = [...$defaults, ...$overrides]; print_r($settings); // "theme" from $overrides wins - later spread wins
Array
(
[theme] => dark
[retries] => 3
)Spreading into function calls
The same ... syntax unpacks an array into a function's positional arguments, which is especially handy when the values you need to pass are already sitting in an array - for example, values collected from user input or built up in a loop.
Unpacking arguments with spread
<?php
function add(int $a, int $b, int $c): int {
return $a + $b + $c;
}
$numbers = [3, 4, 5];
echo add(...$numbers); // 12 - same as add(3, 4, 5)12
list()and[$a, $b] = $arrare interchangeable - the short form is simply the modern convention.Destructuring more variables than the array has elements assigns
null(with a warning) to the extras, rather than raising a fatal error.Spread in an array literal always creates a shallow copy of the source elements - nested arrays inside are still copied by value, same as any PHP array assignment.
Spread works with any
iterable, not just plain arrays, which includes generators and objects implementingTraversable.