Foreach与list-each在PHP中

Is there any difference in the following methods?

$fruit = array(
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cranberry'
);

// List-each method
reset($fruit);
while (list($key, $val) = each($fruit)) {
    echo "$key => $val
";
}

// foreach method
foreach ($fruit as $key => $value) {
    echo "$key => $val
";
}

list start iteration from current element whereas foreach start from the first element.

In case you've already iterated the array, list will start from the next element from the previous iteration. You need to reset the array if you want to iterate from the first element.

As far as I am concerned, there isn't. Except for the foreach() function looking cleaner imo.

each() remembers its position in the array, so if you don't reset() it you can miss items.

reset($array);
while(list($key, $value) = each($array))

For what it's worth this method of array traversal is ancient and has been superseded by the more idiomatic foreach. I wouldn't use it unless you specifically want to take advantage of its one-item-at-a-time nature.

array each ( array &$array )

Return the current key and value pair from an array and advance the array cursor.

After each() has executed, the array cursor will be left on the next element of the array, or past the last element if it hits the end of the array. You have to use reset() if you want to traverse the array again using each.

Read this link for more explanation.