<HTML ><HEAD ><TITLE >each</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Array Functions" HREF="ref.array.html"><LINK REL="PREVIOUS" TITLE="current" HREF="function.current.html"><LINK REL="NEXT" TITLE="end" HREF="function.end.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.current.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.end.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.each" ></A >each</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8581" ></A ><P > (PHP 3, PHP 4 )</P >each -- Return the current key and value pair from an array and advance the array cursor </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8584" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >each</B > ( array array)<BR ></BR ><P > Returns the current key and value pair from the array <TT CLASS="parameter" ><I >array</I ></TT > and advances the array cursor. This pair is returned in a four-element array, with the keys <SPAN CLASS="emphasis" ><I CLASS="emphasis" >0</I ></SPAN >, <SPAN CLASS="emphasis" ><I CLASS="emphasis" >1</I ></SPAN >, <SPAN CLASS="emphasis" ><I CLASS="emphasis" >key</I ></SPAN >, and <SPAN CLASS="emphasis" ><I CLASS="emphasis" >value</I ></SPAN >. Elements <SPAN CLASS="emphasis" ><I CLASS="emphasis" >0</I ></SPAN > and <SPAN CLASS="emphasis" ><I CLASS="emphasis" >key</I ></SPAN > contain the key name of the array element, and <SPAN CLASS="emphasis" ><I CLASS="emphasis" >1</I ></SPAN > and <SPAN CLASS="emphasis" ><I CLASS="emphasis" >value</I ></SPAN > contain the data. </P ><P > If the internal pointer for the array points past the end of the array contents, <B CLASS="function" >each()</B > returns <TT CLASS="constant" ><B >FALSE</B ></TT >. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8606" ></A ><P ><B >Example 1. <B CLASS="function" >each()</B > examples</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese"); $bar = each ($foo);</PRE ></TD ></TR ></TABLE ><P > <TT CLASS="varname" >$bar</TT > now contains the following key/value pairs: <P ></P ><UL COMPACT="COMPACT" ><LI ><SPAN >0 => 0</SPAN ></LI ><LI ><SPAN >1 => 'bob'</SPAN ></LI ><LI ><SPAN >key => 0</SPAN ></LI ><LI ><SPAN >value => 'bob'</SPAN ></LI ></UL > <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$foo = array ("Robert" => "Bob", "Seppo" => "Sepi"); $bar = each ($foo);</PRE ></TD ></TR ></TABLE > </P ><P > <TT CLASS="varname" >$bar</TT > now contains the following key/value pairs: <P ></P ><UL COMPACT="COMPACT" ><LI ><SPAN >0 => 'Robert'</SPAN ></LI ><LI ><SPAN >1 => 'Bob'</SPAN ></LI ><LI ><SPAN >key => 'Robert'</SPAN ></LI ><LI ><SPAN >value => 'Bob'</SPAN ></LI ></UL > </P ></DIV ></TD ></TR ></TABLE > </P ><P > <B CLASS="function" >each()</B > is typically used in conjunction with <A HREF="function.list.html" ><B CLASS="function" >list()</B ></A > to traverse an array; for instance, <TT CLASS="varname" >$_POST</TT >: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8637" ></A ><P ><B >Example 2. Traversing <TT CLASS="varname" >$_POST</TT > with <B CLASS="function" >each()</B > </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >echo "Values submitted via POST method:<br />\n"; reset ($_POST); while (list ($key, $val) = each ($_POST)) { echo "$key => $val<br />\n"; }</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > After <B CLASS="function" >each()</B > has executed, the array cursor will be left on the next element of the array, or on the last element if it hits the end of the array. You have to use <A HREF="function.reset.html" ><B CLASS="function" >reset()</B ></A > if you want to traverse the array again using each. </P ><P > See also <A HREF="function.key.html" ><B CLASS="function" >key()</B ></A >, <A HREF="function.list.html" ><B CLASS="function" >list()</B ></A >, <A HREF="function.current.html" ><B CLASS="function" >current()</B ></A >, <A HREF="function.reset.html" ><B CLASS="function" >reset()</B ></A >, <A HREF="function.next.html" ><B CLASS="function" >next()</B ></A >, <A HREF="function.prev.html" ><B CLASS="function" >prev()</B ></A >, and <A HREF="control-structures.foreach.html" >foreach</A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.current.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.end.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >current</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >end</TD ></TR ></TABLE ></DIV ></BODY ></HTML >