Destructuring
Destructuring is a general programming concept where the individual elements of a compound value, such as an array, a list, or an object, are extracted in one step and bound to separate variables, instead of being accessed one at a time by index or property.
Many languages have dedicated destructuring syntax that also supports skipping elements, providing default values, capturing remaining elements, and destructuring nested structures in a single expression.
Destructuring is provided by list() and its short array syntax [ ] on the left side of an assignment, and by foreach() when iterating over arrays of arrays. PHP does not support destructuring of arbitrary objects, though list() may be combined with ArrayAccess-implementing objects.
<?php
// array destructuring, PHP style
[$a, $b, $c] = [1, 2, 3];
// skipping an element
[$first, , $third] = [1, 2, 3];
// keyed destructuring
['name' => $name, 'age' => $age] = ['name' => 'Ann', 'age' => 30];
// nested destructuring
[[$x, $y], [$z]] = [[1, 2], [3]];
// destructuring while iterating
foreach ([[1, 2], [3, 4]] as [$left, $right]) {
echo $left + $right, "\n";
}
?>