PHP Doku:: Liefert das aktuelle Paar (Schlüssel und Wert) eines Arrays und rückt den Arrayzeiger vor - function.each.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenArraysArray Funktioneneach

Ein Service von Reinhard Neidl - Webprogrammierung.

Array Funktionen

<<current

end>>

each

(PHP 4, PHP 5)

eachLiefert das aktuelle Paar (Schlüssel und Wert) eines Arrays und rückt den Arrayzeiger vor

Beschreibung

array each ( array &$array )

Liefert das aktuelle Schlüssel-Wert-Paare des Arrays array, und erhöht den Arrayzeiger.

Nachdem each() ausgeführt wurde, bleibt der Arrayzeiger beim nächsten Element des Arrays, oder wenn am Ende des Arrays angelangt, nach dem letzten Element. Sie müssen reset() verwenden, wenn Sie das Array erneut mittels each durchgehen wollen.

Parameter-Liste

array

Das Eingabe-Array.

Rückgabewerte

Gibt das aktuelle Schlüssel-Wert-Paar aus dem Array array zurück. Dieses Paar wird als Array mit vier Elementen zurückgegeben, und zwar mit den Schlüsseln 0, 1, key, und value. Die Elemente 0 und key enthalten den Namen des Schlüssels, 1 und value den Wert.

Zeigt der interne Zeiger hinter das Ende der Array-Inhalte, gibt each() FALSE zurück.

Beispiele

Beispiel #1 each()-Beispiele

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

$bar enthält nun die folgenden Schlüssel-Wert-Paare:

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

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

$bar enthält nun die folgenden Schlüssel-Wert-Paare:

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

each() wird typischerweise in Verbindung mit list() verwendet, um ein Array durchzugehen:

Beispiel #2 Durchgehen eines Arrays mit each()

<?php
$fruit 
= array('a' => 'Apfel''b' => 'Banane''c' => 'Preiselbeere');

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

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

a => Apfel
b => Banane
c => Preiselbeere

Achtung

Da die Zuweisung eines Arrays zu einer anderen Variable den originalen Arrayzeiger zurücksetzt, würde unser obiges Beispiel in eine Endlosschleife geraten, hätten wir $fruit innerhalb der Schleife einer anderen Variable zugeordnet.

Warnung

each() akzeptiert ebenfalls Objekte, liefert aber eventuell unerwartete Ergebnisse zurück. Es wird daher nicht empfohlen, über Objekteigenschaften mit each() zu iterieren.

Siehe auch

  • key() - Liefert einen Schlüssel eines assoziativen Arrays
  • list() - Weist Variablen zu, als wären sie ein Array
  • current() - Liefert das aktuelle Element eines Arrays
  • reset() - Setzt den internen Zeiger eines Arrays auf sein erstes Element
  • next() - Rückt den internen Zeiger eines Arrays vor
  • prev() - Verkleinert den internen Zeiger eines Arrays
  • foreach
  • Objektiteration


24 BenutzerBeiträge:
- Beiträge aktualisieren...
massimo dot modica at tin dot it
3.02.2010 11:36
If you forget to reset the array before each(), the same code may give different results with different php versions.

<?php

$a
= array(1,2,3);

foreach (
$a AS $k => $v) $a[$k] = 2*$v;

while(list(
$k2, $v2) = each($a)) { echo($v2."\n"); }

?>

In PHP 5.2.0:

2
4
6

In PHP 5.2.6:

4
6
php at omit dot ianco dot co dot uk
13.04.2008 15:10
I wanted to be able to add to an array while looping through it. foreach does not allow this because it is using a secret copy of the array. each makes this possible (tested on PHP 4).
<?php
$shopping_list
= array('oysters', 'caviare');
reset ($shopping_list);
while (list(
$key, $value) = each ($shopping_list)) {
    if (
$value == 'oysters') $shopping_list[] = 'champagne';
    elseif (
$value == 'champagne') $shopping_list[] = 'ice';
}
print_r($shopping_list);
// Array ( [0] => oysters [1] => caviare [2] => champagne [3] => ice )
?>
Mark Rosenberg
31.08.2007 4:11
manz_2007 at yahoo dot com:

You don't seem to understand what a key is. It is the unique key which references the item in the array. It can't be duplicated. To make it more clear:

array[0]="hello";
array[0]="world";
print(array[0]);

array[0] will obviously print "world". This is because you first set the array element with the key 0 to hello, and then set it to world. The old value is overwritten.

In your example hire_date is a key, just like 0 is. You set the value of it twice, and of course the second value overwrites the first.

You can of course enter duplicate values into an array, but not keys. Think about it, how would you reference two different values with one key?
manz_2007 at yahoo dot com
28.08.2007 9:02
We have an array like this :

$aField = array("eid" => "c", "name" => "l", "department" => "l", "hire_date" => "d", "hire_date" => "sd", "actives" => "i");

we want to walking every value like :
eid => c
name => l
department => l
hire_date => d
hire_date => sd
actives => i

but when using

foreach($aField as $key => $value)
{ echo "$key, $value"; }

or using
print_r($aField);

its look like this:
eid => c
name => l
department => l
hire_date => sd
actives => i

BTW, the hire_date => d as we expected is missing...

Anybody can resolve this matter ?

Thanks
Man'z
wolfeym38 at yahoo dot com
12.06.2007 8:28
Regarding speed of foreach vs while(list) =each
I wrote a benchmark script and the results are that clearly foreach is faster. MUCH faster. Even with huge arrays (especially with huge arrays). I tested with sizes 100,000. 1,000,000 and 10,000,000. To do the test with 10 million i had to set my memory limit real high, it was close to 1gb by the time it actually worked. Anyways,

<?php
function getDiff($start, $end) {
   
$s = explode(' ', $start);
   
$stot = $s[1] + $s[0];
   
$e = explode(' ', $end);
   
$etot = $e[1] + $e[0];
    return
$etot - $stot;
}

$lim=10000000;
$arr = array();
for (
$i=0; $i<$lim; $i++) {
   
$arr[$i] = $i/2;
}

$start = microtime();
foreach (
$arr as $key=>$val);

$end = microtime();
echo
"time for foreach = " . getDiff($start, $end) . ".\n";

reset($arr);
$start = microtime();
while (list(
$key, $val) = each($arr));
$end = microtime();
echo
"time list each = " . getDiff($start, $end) . ".\n";
?>

here are some of my results: with 1,000,000
time for foreach = 0.0244591236115.
time list each = 0.158002853394.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 0.0245339870453.
time list each = 0.154260158539.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 0.0269000530243.
time list each = 0.157305955887.

then with 10,000,000:
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 1.96586894989.
time list each = 14.1371650696.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 2.02504014969.
time list each = 13.7696218491.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 2.0246758461.
time list each = 13.8425710201.

by the way, these results are with php 5.2 i believe, and a linux machine with 3gb of ram and 2.8ghz dual core pentium
michael k (name der redaktion bekannt.)
10.05.2007 21:36
If you want to iterate over a two-dimensional, sparse array, and want to  first display every first element, then every second and so on, you can use this code:

 $fruits = array ( "fruits"  => array ( "a" => "orange",
                                      "b" => "banana",
                                      "c" => "apple"
                                    ),
                 "numbers" => array ( 1,
                                      2,
                                      3,
                                      4,
                                      5,
                                      6
                                    ),
                 "holes"   => array (      "first",
                                      5 => "second",
                                           "third",
                                     10 => "fourth",
                                    )
               );

$done = False;
while ($done == False) {       
       $done = True;

       // Important: &$val has to be a reference (use the &),
       // if you don't, the internal counter of $val will be
       // re-initialized each time and you loop over the first elements
       // for eternity.

       foreach($fruits as $key => &$val) {

               if (list($inner_key, $inner_val) = each(&$val)) {
                       $done = False;
                       echo "$key : : $inner_key => $inner_val  <br>  \n";
               }

       }
}

NOTE: this is just a quick hack, if you know a better way, post it!
Bleach984
10.11.2006 2:43
have found something interesting.

when I use a FOREACH with the WHILE statement inside it as talk has been going on,

it will give you all of the values of a multi-dimensional array.

like this

<?php
$categories
= return_cats();

foreach (
$categories as $category ) {

       while(list(
$k, $v) = each ($category)) {
     
       echo
"$k ... $v <br />";
     
       }

}
?>
will output something like this

Truecat_name ... Awesome
category_nicename ... awesome
cat_count ... 3
lastday ... 28
lastmonth ... 10
cat_name ... Uncategorized
category_nicename ... uncategorized
cat_count ... 1
lastday ... 28
lastmonth ... 10
cat_name ... Welcome Messages
category_nicename ... welcome-messages
cat_count ... 3
lastday ... 28
lastmonth ... 10
Michael
29.07.2006 22:02
Something that I found useful to note is that each() does NOT return a reference to the array contets, but a copy of the item.

So, say you have:
$boxes = Array();
$boxes["Large"] = Array();
$boxes["Medium"] = Array();
$boxes["Small"] = Array();

and want to put the small box inside the medium box inside the large box, doing:

$lastBox = NULL;
while (list($key, $box) = each($boxes)) {
  if (isset($lastBox))
    $lastBox[0] =& $box;
  $lastBox =& $box;
}

will not work.  Instead, you have to use the key like:

$lastBox = NULL;
while (list($key) = each($boxes)) {
  $box =& $boxes[$key];
  if (isset($lastBox))
    $lastBox[0] =& $box;
  $lastBox =& $box;
}

which will work.
scin
4.05.2006 5:29
Regarding the user who states that foreach is faster, this depends on the size of the array you are dealing with.

On large arrays while(list($key, $val) = each($array) is considerably faster, as in 10x faster with arrays that number in the millions of elements.
sjoerd-php at linuxonly dot nl
31.03.2006 16:04
Use foreach instead of while, list and each. Foreach is:
- easier to read
- faster
- not influenced by the array pointer, so it does not need reset().

It works like this:
<?php
$arr
= array('foo', 'bar');
foreach (
$arr as $value) {
    echo
"The value is $value.";
}

$arr = array('key' => 'value', 'foo' => 'bar');
foreach (
$arr as $key => $value) {
    echo
"Key: $key, value: $value";
}
?>
matthew at mhalls dot net
5.12.2005 0:58
To panania at 3ringwebs dot com:

If you know for certain that you are only receiving one row, the while becomes redundant. To shorten your code:

$strSQL = "SELECT * FROM table WHERE id=1";
$RecordsetSelect = $db->runQuery ($strSQL);
list($key, $val) = mysql_fetch_row($RecordsetSelect);
echo "$key => $val\n";
mysql_free_result($RecordsetSelect);

With only one row being returned this is more elegant a solution, but just being nit-picky in essence. It also shows another quick way of using list.
amby2 at izh dot com
24.11.2005 18:55
I've found a compact way to cycle through an associative array using for statement (not while, as it has been done in the most of examples below):

<?php

for (reset($array); list($key) = each($array);) {
  echo
$key;
  echo
$array[$key];
}

?>

or

<?php

for (reset($array); list($key, $value) = each($array);) {
  echo
$key;
  echo
$value;
  echo
$array[$key];
}

?>

You hardly forget to add reset($array) code line using such construction.
Sopinon
23.07.2005 14:17
If you want to display the hole structure (tree) of your array, then you can use this recursive solution.

<?PHP
$tree
= "";
array_tree($your_array);
echo
$tree;

// Recursive Function
function array_tree($array, $index=0){
    global
$tree;
   
$space="";
    for (
$i=0;$i<$index;$i++){
       
$space .= "     ";
    }
    if(
gettype($array)=="array"){
       
$index++;
        while (list (
$x, $tmp) = each ($array)){
           
$tree .= $space."$x => $tmp\n";
           
array_tree($tmp, $index);
        }
    }
}
?>
james at gogo dot co dot nz
2.11.2004 16:29
It's worth noting that references to an array don't have thier own array pointer, and taking a reference to an array doesn't reset it's array pointer, so this works as you would expect it would by eaching the first three items of the array, rather than the first item 3 times.

<?php
  $x
= array(1,2,3);

 
print_r(each($x));
  echo
"\n";
 
 
$y =& $x;
 
print_r(each($y));
  echo
"\n";
 
 
$z =& $y;
 
print_r(each($z));
  echo
"\n";
?>
panania at 3ringwebs dot com
1.01.2004 2:51
The last method for record sets is great if you don't know the number of rows returned from a query, but if you do there's an easier way...

$strSQL = "SELECT * FROM table WHERE id=1";
$RecordsetSelect = $db->runQuery ($strSQL);
$row_Recordset1 = mysql_fetch_assoc($RecordsetSelect);
while (list($key, $val) = each($row_Recordset1)) {
    echo "$key => $val\n";
}       
mysql_free_result($RecordsetSelect);

Here we know that only 1 record will be returned because of the WHERE criteria. (Of course this is dependent on you own DB schema.)
wodzuY2k at anronet dot pl
4.08.2002 21:41
This function will help you dump any variable into XML structure.

        //dump var into simple XML structure
        function var_dump_xml($tagname,$variable,$level=0)
         {
            for($i=0;$i<$level;$i++) $marg.=' ';
            if (eregi('^[0-9].*$',$tagname)) $tagname='tag_'.$tagname; //XML tag cannot start with [0-9] character
            if (is_array($variable))
             {
                echo $marg."<$tagname>\n";
                while (list ($key, $val) = each ($variable))  var_dump_xml($key,$val,$level+1);
                echo $marg."</$tagname>\n";
             }
            elseif (strlen($variable)>0)
             {
                 echo $marg."<$tagname>".htmlspecialchars($variable)."</$tagname>\n";
             };    
         };
        
        /*
        example:
       
        $myVar = array("name"=>"Joe", "age"=>"26", "children"=>array("Ann","Michael"));
        var_dump_xml("myVarTag",$myVar);
        */
Gillis at dancrea dot com
26.05.2002 19:13
I wrote a short and pretty simple script to search through associative arrays for some value in the values, heres a simplifyed example of it:

<?php

$foo
['bob'] = "bob is ugly";
$foo['bill'] = "bill is rich";
$foo['barbie'] = "barbie is cute";
$search = "rich";

echo
"searching the array foo for $search:<br>";
reset ($foo);
while (list (
$key, $val) = each ($foo)) {
if (
preg_match ("/$search/i", $val)) {
    print
"A match was found in $key.<br />";
} else {
    print
"A match was not found in $key.<br />";
}
}

?>

will output:
Searching the array foo for rich:
A match was not found in bob
A match was found in bill
A match was not found in barbie
wodzuY2k at anronet dot pl
8.05.2002 18:40
Yet another useful example of how to make globals out of the config file.

config file:
[section1]
 value=foo

PHP result :
variable: $section1_value=foo;

function servicesConfig_makeGlobals()
 {
    global $servicesConfig;
    $servicesConfig = parse_ini_file("servicesConfig.conf", TRUE);
    reset($servicesConfig);
    while (list ($section, $section_val) = each ($servicesConfig))
     {
         reset($servicesConfig[$section]);
        while (list ($key, $val) = each ($servicesConfig[$section]))
         {
            $GLOBALS[$section.'_'.$key]=$val;
         };   
     };   
 };

13.02.2002 13:10
I usually work a lot with 2D arrays. Since I've had some trouble traversing them correctly maybe someone out there also experienced those problems and can use this one.

It's based on a 2D-array called $array[$x][$y]. At some (but not necessarily all) (x,y) there is a value I want to reach. Note that I do not know beforehand the ranges of $x or $y (that is their highest and lowest values).

while (list ($x, $tmp) = each ($array)) {
   while (list ($y, $val) = each ($tmp)) {
      echo "$x, $y, $val";
   }
}

The answer for each (x,y) pair can thus be (providng, of course those values where in your array beforehand):

1, 1, 2
2, 2, 0
3, 1, 1
5, 2, 2
5, 1, 2

Note that only the (x,y) pairs with a corresponding value is shown.

Hang in there
Jon Egil Strand
NTNU
tk at turtle-entertainment dot de
29.01.2001 20:33
Be sure to use the integrated functions "unset();" or "reset();" - many people forget this and wonder about the created output!
kris at angelanthony dot com
25.10.2000 22:03
Remember to use "reset()" if you iterate over an array with "each()" more than once!  Example:

while(list($key,$value) = each($array)){
 // code here
}

NOW the internal pointer on $array is at the end of the array, and another attempt at an iteration like the one above will result in zero executions of the code within the "while" block.  You MUST call "reset($array)" to reset the internal array pointer before iterating over the array again from the first element.
phpmanual at devin dot com
15.07.2000 2:26
Note further that each() performs a shallow copy
of the value side of an (assoc) array; if you need to iterate on deep structures, something like:

<?php

reset
($A); while (list($k,) = each($A)) {
   
$elem = &$A[$k];
   
/* ... */
}

?>

Is needed.  Copy construction would be handy to have here.
ellenzhg at hotmail dot com
30.03.2000 10:06
<?php
//each.php

$foo = array( "Robert" => "Bob", "Seppo" => "Sepi" );
$bar = each( $foo );
$k = implode(array_keys($bar),",");
$v = implode(array_values($bar),",");
echo
$k ."<br>";
echo
$v;
?>

output:
1,value,0,key
Bob,Bob,Robert,Robert
Above example only be interpretered by PHP4.
pyxl at dont_spam_on_me dot jerrell dot com
16.02.2000 12:41
Ok, for you folks who are learning this, here's something that should help your comprehension of each(), because I bashed my brains for a while on this one.

The first example indicates that each() spits out a 4-cell 1 dimensional array.  This is all fine and dandy until you get to the second example, where that seems to be thrown out the window, because though each() is still spitting out 4 array elements, the list() being used is set up to only accept 2 values, as it's being executed with only wo variables in it!

For some folks, this might not be a problem, but I couldn't understand the mismatch - why was it done, and where did the array go that each() generated??  Well, upon executing that code, it turns out that the first two array elements of the 4 element array that each() creates are assigned to those two variables, and the last two array element values are just thrown away - they're totally ignored.  It's how PHP is written.

Now, why do that?  Well, the example was definitely written more to show folks how to use each() to make life much easier when dealing with a particular operations array in PHP that a lot of people work with, but it also has the side effect (which hopefully my little explaination has made more palatable) of demonstrating how each() can act when being used with other functions that don't necessarily want all of each()'s input.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)