★ wanayoo — archive 1999 http://www.php.net/manual/fr/function.each.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Tableaux
Quick Reference
English version of this pageGerman version of this pageJapanese version of this pageItalian version of this pageHungarian version of this page
Tableaux
* array
* array_count_values
* array_flip
* array_keys
* array_merge
* array_pad
* array_pop
* array_push
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort
Manual: each
View the source code for this pageSearch the site



Previous page
 current
 Updated
Sun, 06 Aug 2000
end 
Next page


each

(PHP3 , PHP4 )

each --  Retourne chaque paire clé/valeur d'un tableau

Description

array each (array array)

Retourne la paire (clé/valeur) courante du tableau array et avance le pointeur de tableau. Cette paire est retournée dans un tableau de 4 éléments, avec les clés 0, 1, key, et value. Les éléments 0 et key contiennent le nom de la clé et, et 1 and value contienent la valeur.

Si le pointeur interne de fichier est au dela de la fin du tableau, each() retourne faux.

Exemple 1. Exemples avec each()


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

$bar contient maintenant les paires suivantes:

  • 0 => 0
  • 1 => 'bob'
  • key => 0
  • value => 'bob'

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

$bar contient maintenant les paires suivantes:

  • 0 => 'Robert'
  • 1 => 'Bob'
  • key => 'Robert'
  • value => 'Bob'

each() est utilisé conjointement avec list() pour étudier tous les éléments d'un tableau; par exemple, $HTTP_POST_VARS:

Exemple 2. Affichage de $HTTP_POST_VARS avec each()


echo "Valeurs transmises par la méthode POST:<br>";
reset ($HTTP_POST_VARS);
while (list ($key, $val) = each ($HTTP_POST_VARS)) {
    echo "$key => $val<br>";
}
      

Après chaque each(), le pointeur de tableau est déplacé au dernier éléments, ou sur le dernier élément, lorsqu'on arrive à la fin.

Voir aussi key(), list(), current(), reset(), next(), et prev().


User Contributed Notes: each


royapav@millsaps.edu
24-May-1999 10:02
Tests show that when an array variable is copied (using "=", or when passed as a value parameter to a function), that the internal array pointer is RESET *in the copy*. However, if passed as a reference parameter to a function, the pointer is not changed. (PHP 3.0.7)


joe.kelley@webworlds.net
01-Oct-1999 07:49
When working with databases and resultsets. I have found that each does not return NULL values. This is very annoying for generic queries, where you need to know a complete list of field values be they NULL or not!


jones@commerce.uq.edu.au
17-Nov-1999 01:09
I would have found it useful if the example section had concrete examples of how to actually use the array variables.

eg.
<pre>
foo = array( "Robert" => "Bob", "Seppo" => "Sepi" );
$bar = each( $foo );
echo "key = $bar[key] .... value = $bar[value] ";
</pre>

followed by a note saying what would be printed as a result. Don't make people have to type examples in to see what they print. That's no help to people reading printed versions of documentation.

/\ndy

ps. the extra echo command prints
"key = Robert .... value = Bob"



php-traversing-matrices@mark.datasys.net
28-Nov-1999 08:47
Traversing multidimensional arrays is
easy, especially if you know how many dimensions are involved. Suppose you have a structure returned from a database query, such as
<pre>
$returned_rows[$row_number][$field_name]
</pre>

At least as of version 4 beta 2, each()
doesn't like this:
<pre>
each($returned_rows[$row_number])
</pre>
So I don't know of a way to easily retrieve the keys of the associative
array (logically) at
<pre>
$returned_rows[$row_number]
</pre>
(I say `logically' because I'm not sure that this is meaningful PHP.)

Thus, to walk through each field, you
might try this:
<pre>


while (
list($row_number, $row_array)
= each($query_result_array)
) {
while (
list($field_name, $field_value)
= each($row_array)
) {
##
## $row_number has the
## current row number,
## $field_name hsa the current
## field' name,
## $field_value has the current ## field's value
}
}
}



steve@juggler.net
07-Feb-2000 01:02
As of PHP 4.0b3, the interpreter's not quite smart enough to manage the following:<PRE>
while(list($k,$v)=each(array("first","second","third")) {
</PRE>
Instead, use <PRE>
$ary=array("first","second","third");
while (list($k,$v)=each($ary)) {
</PRE>



pyxl@dont_spam_on_me.jerrell.com
16-Feb-2000 03: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.



evan@dig-e-tal.com
17-Feb-2000 07:28
I would be nice if <pre>each(non-array variable)</pre> returned that same non-array variable, as opposed to giving a warning that isn't useful at all to me. That way I wouldn't have to test each time for arrays when I don't need to.


sculptor@ve-studio.com
06-Mar-2000 06:31
I tried the example code:

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

along with others similar and I keep getting the error "Warning: Variable passed to reset() is not an array or object in files/test.inc on line 3"

Anyone have any idea why the "$HTTP_POST_VARS" is not being accepted as a variable? (Yes, this code was called after a POST form.)



ridcully@magnet.at
22-Mar-2000 06:03
To output all entries of an array $a, do this
<PRE>
reset ($a);
while( $res=each($a) )
{
echo "$res[1]
";
};
</pre>
If you need the keys too, it's in $res[0].



zombie@localm.org
25-Mar-2000 11:42
We're converting our perl site to PHP and I'm really stuck on switching "foreach" statements to "for" statements.

I'm using flat cheesy colon deliminated databases. so like what would the php equiv of the below perl line be? please mail me. thanks.

<PRE> foreach $data (@data) {
@info = split("::", $data);</PRE>

}
data=each line in the file and
@info being each field.



ellenzhg@hotmail.com
30-Mar-2000 12:06
<pre>
&lt;?php
/each.php
echo "

";
$foo = array( "Robert" => "Bob", "Seppo" => "Sepi" );
$bar = each( $foo );
$k = implode(array_keys($bar),",");
$v = implode(array_values($bar),",");
echo $k . "
";
echo $v;
?>
</pre>
output:
1,value,0,key
Bob,Bob,Robert,Robert
Above example only be interpretered by PHP4.



mark@nospam@art-nude.com
13-Apr-2000 05:11
If you're using the each() function, and having trouble getting the results you want, be sure to check out the mysql_fetch_array result types...

Do you want:
<PRE>
this: $profile = mysql_fetch_array( $query_result , MYSQL_ASSOC); ?
this: $profile = mysql_fetch_array( $query_result , MYSQL_NUM); ?
or this: $profile = mysql_fetch_array( $query_result , MYSQL_BOTH); ?
</PRE>



jasonp019@yahoo.com
17-May-2000 07:05
each() is apparently a destructive operator. The following code:

<PRE>
&lt;?php
class foo {
var $data;

function foo()
{
$this->data = array(1, 2);
}

function get_data()
{
while (list($key, $val) = each($this->data)) {
$str .= "$val ";
}
return $str;
}
}

$foo1 = new foo;
print $foo1->get_data()."
\n";
print $foo1->get_data()."
\n";
?>
</PRE>
Results in the following output:

1 2

The expected output is:

1 2
1 2

Why is each() destructive? This is php version 3.0.12.

PS: Replacing the following statement in the above code:
<PRE>
while (list($key, $val) = each($this->data)) {
</PRE>
with:
<PRE>
$test = $this->data;
while (list($key, $val) = each($test)) {
</PRE>
produces the correct output.

Thanks!



phpmanual@devin.com
14-Jul-2000 05: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:

reset($A); while (list($k,) = each($A)) {

$elem = &$A[$k];

/* ... */

}

Is needed. Copy construction would be handy to have here.



aryn@aryn.nu
28-Jul-2000 08:34
one way to get variables out of an array dynamically, while skipping the keys is this:


while( list($key,$value)=each($foo)) {

if (!is_int($key)) {

echo "$"."$key: $value\n";

$$key = $value;

}

}

(the echo line is just for displaying what is variables are getting set; ie: not needed.)



F.Dijkstra@phys.uu.nl
08-Aug-2000 10:11
In repsonse to jasonp019@yahoo.com:

You need to reset() the array before you call it using each().



 About Notes


Previous page
 current
 Updated
Sun, 06 Aug 2000
end 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.