This is a working example code from php doc for using list() with each()
<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo "$key => $val\n";
}
?>
The output is
a => apple
b => banana
c => cranberry
I don’t understand why it works and output that way. Because,
from the API of each(), it says each() return an array with 4 key/value pair.
So each($fruit) should be like this:
Array
(
[1] => yy
[value] => yy
[0] => xx
[key] => xx
)
for the code:
while (list($key, $val) = each($fruit)) {
echo "$key => $val\n";
}
shouldn’t $key equal to each($fruit)[1], and $val equal to each($fruit)[value]?
In other words,
listactively looks for numerical keys in order.list($foo, $bar)requires that the assigned array has the keys0and1and will assign those to$fooand$barrespectively. It doesn’t matter which order these keys are in in the array itself.