each
each
Return the current key and value pair from an array and advance the array cursor
array **each** $array
Return the current key and value pair from an array and advance the array cursor.
After 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
if you want to traverse the array
again using each.
each``reset
arrayThe input array.
Returns the current key and value pair from the array
. This pair is returned in a four-element
array, with the keys , ,
, and . Elements
and contain the key name of
the array element, and and
contain the data.
array``0``1``key``value``0``key``1``value
If the internal pointer for the array points past the end of the
array contents, returns
false.
each
Voorbeeld: examples
<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>
now contains the following key/value
pairs:
`$bar`
Array
(
[1] => bob
[value] => bob
[0] => 0
[key] => 0
)
<?php
$foo = array("Robert" => "Bob", "Seppo" => "Sepi");
$bar = each($foo);
print_r($bar);
?>
now contains the following key/value
pairs:
`$bar`
Array
(
[1] => Bob
[value] => Bob
[0] => Robert
[key] => Robert
)
is typically used in conjunction with
to traverse an array, here's an
example:
each``list**Voorbeeld: Traversing an array with **
<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo "$key => $val\n";
}
?>
a => apple
b => banana
c => cranberry
Let op: > Because assigning an array to another variable resets the original array's pointer, our example above would cause an endless loop had we assigned to another variable inside the loop.
$fruit
Waarschuwing: > will also accept objects, but may return unexpected results. It's therefore not recommended to iterate though object properties with .
each``each
key``list``current``reset``next``prevObject Iteration