Traversing Arrays Using list() and each() in PHP

By: Andi, Stig and Derick Printer Friendly Format    

Although foreach() is the nicer way of iterating over an array, an additional way of traversing an array is by using a combination of the list() construct and the each() function:

$players = array("John", "Barbara", "Bill", "Nancy");


while (list($key, $val) = each($players)) {

print "#$key = $val\n";


The output of this example is

#0 = John

#1 = Barbara

#2 = Bill

#3 = Nancy

Most Viewed Articles (in PHP )

Latest Articles (in PHP)

Comment on this tutorial