Destructuring arrays using list

suggest change

Use list() to assign values from an array to a list of variables. See also compact(), which builds an array from variables.

// Assigns to $a, $b and $c the values of their respective array elements in           $array with keys numbered from zero
list($a, $b, $c) = $array;

Since PHP 7.1 you can use short list syntax, including keys:

// Assigns to $a, $b and $c the values of their respective array elements in $array with keys numbered from zero
[$a, $b, $c] = $array;
// Assigns to $a, $b and $c the values of the array elements in $array with the keys "a", "b" and "c", respectively
["a" => $a, "b" => $b, "c" => $c] = $array;

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents