PHP3 Manual
PrevNext

each

each -- return next key/value pair from an array

Description

array each(array array);

Returns the current key/value pair from the array array and advances the array cursor. This pair is returned in a four-element array, with the keys 0, 1, key, and value. Elements 0 and key each contain the key name of the array element, and 1 and value contain the data.

Example 1. each() examples

$foo = array( "bob", "fred", "jussi", "jouni" );
$bar = each( $foo );
      

$bar now contains the following key/value pairs:

  • 0 => 0

  • 1 => 'bob'

  • key => 0

  • value => 'bob'

$foo = array( "Robert", => "Bob", "Seppo" => "Sepi" );
$bar = each( $foo );
       

$bar now contains the following key/value pairs:

  • 0 => 'Robert'

  • 1 => 'Bob'

  • key => 'Robert'

  • value => 'Bob'

each() is typically used in conjunction with list() to traverse an array; for instance, $HTTP_POST_VARS:

Example 2. Traversing $HTTP_POST_VARS with each()

echo "Values submitted via POST method:<br>";
while ( list( $key, $val ) = each( $HTTP_POST_VARS ) ) {
   echo "$key => $val<br>";
}
      

See also key(), current(), reset(), next(), and prev().


PrevHomeNext
currentUpend