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

By: Andi, Stig and Derick  

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");

reset($players);

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 )

Multiple File Upload in PHP using IFRAME

Reading word by word from a file in PHP

Function to sort array by elements and count of element in PHP

Warning: session_start(): open .... failed - PHP error

Locking files in PHP

error: "Service Unavailable" after installing PHP to a Windows XP x64 Pro

Cannot load /usr/local/apache/libexec/libphp4.so into server: ld.so.1:......

Installing PHP 5.x with Apache 2.x on HP UX 11i and configuring PHP 5.x with Oracle 9i

Anonymous functions as properties in Classes in PHP

Counting Lines, Paragraphs, or Records in a File using pc_split_paragraphs() in PHP

Reading .ini files in PHP

Password protecting a page in PHP

Using Text file as database in PHP

Setting up PHP in Windows 2003 Server IIS7, and WinXP 64

Running different websites on different versions of PHP in Windows 2003 & IIS6 platform

Latest Articles (in PHP)

Comment on this tutorial