Native resource: the list
This feature exists since PHP 4, is the list()
. It’s that simple:
list($variavel1, $variavel2, $variavel3) = $array;
In newer Phps it accepts this simplified syntax, but it is the same Construct:
[$variavel1, $variavel2, $variavel] = $array;
Since PHP 7.1 you can choose the items with indexes (beware, starts from 0):
list(1 => $variavel2, 2 => $variavel3) = $array;
[1 => $variavel2, 2 => $variavel3] = $array;
Or omit variables you don’t need:
list( , $variavel2, $variavel3 ) = $array;
Example:
$array = [
'UM',
'DOIS',
'TRES'
];
list($variavel1, $variavel2, $variavel3) = $array;
echo "Var 1: $variavel1\n";
echo "Var 2: $variavel2\n";
echo "Var 3: $variavel3\n";
Upshot:
Var 1: UM
Var 2: DOIS
Var 3: TRES
See working on IDEONE
Handbook:
https://www.php.net/manual/en/function.list.php
Related: https://answall.com/q/486846/69296
– Luiz Felipe
This answers your question? It is possible to unstructure an array in PHP equal to or similar to the Python list?
– Luiz Felipe