The role of the list function in PHP is to assign the values in the array to some variables, so that we can use the list to implement simple data operations. Oh, you can refer to it if necessary.
Note: list() can only be used on numerically indexed arrays and assumes that numerical indexing starts at 0.
Example 1. list() example
代码如下 | 复制代码 |
$info = array('coffee', 'brown', 'caffeine'); // Listing all the variables list($drink, $color, $power) = $info; echo "$drink is $color and $power makes it special.n"; // Listing some of them list($drink, , $power) = $info; echo "$drink has $power.n"; // Or let's skip to only the third one list( , , $power) = $info; echo "I need $power!n"; ?> |
Example 2. Example of using list()
代码如下 | 复制代码 | ||||
|
Warning
list() assigns values starting from the rightmost parameter. If you use simple variables, don't worry about this. But if you use an indexed array, usually you expect the result to be left to right as written in list(), but it's not. are assigned in reverse order.
Example 3. Using array index in list()
The code is as follows
|
Copy code
|
||||
代码如下 | 复制代码 | ||||
array(3) { |
The code is as follows | Copy code |
array(3) { [2]=> string(8) "caffeine" [1]=> string(5) "brown" [0]=> string(6) "coffee" } |