|
listAssign variables as if they were an array Description
array list(mixed
$var , mixed ...$vars = ?)Like array, this is not really a function, but a language construct. list is used to assign a list of variables in one operation. Strings can not be unpacked and list expressions can not be completely empty.
As of PHP 7.1.0, list can also contain explicit keys, allowing for the destructuring of arrays with non-integer or non-sequential keys. For more details on array destructuring, see the array destructuring section. Parameters
Return ValuesReturns the assigned array. Changelog
Examples
Example #1 list examples
Example #2 An example use of list
Example #3 Using nested list
int(1) int(2) int(3)
Example #4 list and order of index definitions The order in which the indices of the array to be consumed by list are defined is irrelevant.
Gives the following output (note the order of the elements compared in which order they were written in the list syntax): array(4) { [2]=> string(1) "a" ["foo"]=> string(1) "b" [0]=> string(1) "c" [1]=> string(1) "d" } string(1) "c" string(1) "d" string(1) "a"
Example #5 list with keys As of PHP 7.1.0 list can now also contain explicit keys, which can be given as arbitrary expressions. Mixing of integer and string keys is allowed; however, elements with and without keys cannot be mixed.
The above example will output: id: 1, name: Tom id: 2, name: Fred 2, 4 See Also
|