No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Rechercher une fonction PHP

each

(PHP 4, PHP 5, PHP 7)

eachReturn the current key and value pair from an array and advance the array cursor

Warning

This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.

Description

each ( array &$array ) : array

Return the current key and value pair from an array and advance the array cursor.

After each() 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 reset() if you want to traverse the array again using each.

PHP: each - Manual Home of Manuel PHP  Contents Haut

Parameters

array

The input array.

PHP: each - Manual Home of Manuel PHP  Contents Haut

Return Values

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

If the internal pointer for the array points past the end of the array contents, each() returns FALSE.

PHP: each - Manual Home of Manuel PHP  Contents Haut

Examples

Example #1 each() examples

<?php
$foo 
= array("bob""fred""jussi""jouni""egon""marliese");
$bar each($foo);
print_r($bar);
?>

$bar now contains the following key/value pairs:

Array
(
    [1] => bob
    [value] => bob
    [0] => 0
    [key] => 0
)

<?php
$foo 
= array("Robert" => "Bob""Seppo" => "Sepi");
$bar each($foo);
print_r($bar);
?>

$bar now contains the following key/value pairs:

Array
(
    [1] => Bob
    [value] => Bob
    [0] => Robert
    [key] => Robert
)

each() is typically used in conjunction with list() to traverse an array, here's an example:

Example #2 Traversing an array with each()

<?php
$fruit 
= array('a' => 'apple''b' => 'banana''c' => 'cranberry');

reset($fruit);
while (list(
$key$val) = each($fruit)) {
    echo 
"$key => $val\n";
}
?>

The above example will output:

a => apple
b => banana
c => cranberry

Caution

Because assigning an array to another variable resets the original array's pointer, our example above would cause an endless loop had we assigned $fruit to another variable inside the loop.

Warning

each() will also accept objects, but may return unexpected results. It's therefore not recommended to iterate though object properties with each().

PHP: each - Manual Home of Manuel PHP  Contents Haut

See Also

  • key() - Fetch a key from an array
  • list() - Assign variables as if they were an array
  • current() - Return the current element in an array
  • reset() - Set the internal pointer of an array to its first element
  • next() - Advance the internal pointer of an array
  • prev() - Rewind the internal array pointer
  • foreach
  • Object Iteration

Find a PHP function

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.each.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : http://php.net

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut