This may come as a bit of a shock to you, but PHP's arrayfunctions can do a lot more than just count the elements of an array oriterate through key-value pairs. This article takes an in-depth look atPHP's less well-known array manipulation tools, illustrating how they canbe used to write tighter, more efficient code...and have some fun in thebargain as well!
A number of built-in functions are available for use while iterating through an array - you'll typically use these in combination with a "while" or "for" loop.
The current() function returns the currently-in-use element of an array (beginning with the first element),
while the each() function comes in handy when you need to
iterate through an array.
<?
// create array
$music = array("pop", "rock", "jazz", "blues");
// creates the array ("0" => 0, "1" => "pop", "key" => 0, "value" => "pop")
$this = each($music);
// returns "0" - no key, as this is not a hash
echo $this["0"];
// returns "pop"
echo $this["1"];
// returns "0" - no key, as this is not a hash
echo $this["key"];
// returns "pop"
echo $this["value"];
?>
Confused? Don't be - every time each() runs on an array, it
creates a hash containing four keys: "0", "1", "key" and "value". The "0" and "key" keys contain the name of the currently-in-use key of the array (or hold the value 0 if the array does not contain keys), while the "1" and "value" keys contain the corresponding value.