PHP Reference ยท Language Constructs and Statements
PHP list() and array destructuring
Reference for PHP list() and [] array destructuring: unpack arrays into variables by position or key, skip items, nest patterns, swap values and use foreach.
Destructuring assigns several variables from an array in one statement. You write a pattern on the left of =, and PHP copies the matching elements out of the array on the right. The pattern can use the short form [$a, $b] or the older list($a, $b); they mean the same thing.
Patterns work by position ([$x, $y]) or by key (['id' => $id]), can skip elements, can nest, and can be used in foreach. Missing elements become null with a warning.
[$first, $second] = [10, 20]; ['name' => $name, 'age' => $age] = ['name' => 'Ada', 'age' => 36];
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
[$a, $b] = $array; list($a, $b) = $array; ['key' => $a, 'other' => $b] = $array; list('key' => $a) = $array;
| Name | Description |
|---|---|
| Pattern | A list of targets, each usually a variable, but it can also be an array element or property ($arr['k'], $obj->p), a nested pattern, or empty to skip an element. |
| Key => target | Selects the element by key instead of position. A pattern must be all positional or all keyed; mixing them is a compile error. |
&$target |
Binds by reference (PHP 7.3+). |
Return value: the assignment expression evaluates to the right-hand array.
Errors: a missing index or key gives a warning ("Undefined array key") and null. Destructuring a non-array (except null) assigns null silently; with a string it does nothing useful.
Usage notes
- Short syntax is the norm.
[$a, $b] = ...is clearer thanlist().list()remains valid but adds nothing. - Order of assignment. For positional patterns, elements are assigned left to right. In
[$a, $b] = [$b, $a]PHP evaluates the right side first, which is why swapping works. - Associative arrays need keys. Destructuring
['x' => 1]with a positional pattern reads index0and finds nothing. - Strings are not unpacked.
[$a, $b] = "ab"does not split the string; usestr_split(). - Skipping. Leave a slot empty (
[, $b]) to ignore an element. - Nested and non-variable targets are allowed:
[[$a, $b], $c] = ...and[$obj->x, $arr['y']] = [1, 2]. ArrayAccessobjects can be destructured too; PHP callsoffsetGet()for each element.- Parameters. Destructuring does not work in a function signature; unpack inside the body.
- Related to spread.
...unpacks arrays into arguments or other arrays, a different feature; see Spread operator.