Destructuring arrays using list
suggest changeUse 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;
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents