<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >each</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="Arrays" 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=UTF-8"></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" >Manual do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.current.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.end.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.each" ></A >each</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN12433" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >each -- Retorna o par chave/valor corrente de um array e avança o seu cursor </DIV ><DIV CLASS="refsect1" ><A NAME="AEN12436" ></A ><H2 >Descrição</H2 >array <B CLASS="methodname" >each</B > ( array array )<BR ></BR ><P > Retorna o par chave/valor corrente de <CODE CLASS="parameter" >array</CODE > e avança o seu cursor. Esse par é retornado num array de quatro elementos, com as chaves <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 >, e <SPAN CLASS="emphasis" ><I CLASS="emphasis" >value</I ></SPAN >. Os elementos <SPAN CLASS="emphasis" ><I CLASS="emphasis" >0</I ></SPAN > e <SPAN CLASS="emphasis" ><I CLASS="emphasis" >key</I ></SPAN > contêm o nome da chave do elemento do array, e <SPAN CLASS="emphasis" ><I CLASS="emphasis" >1</I ></SPAN > e <SPAN CLASS="emphasis" ><I CLASS="emphasis" >value</I ></SPAN > contêm o valor. </P ><P > Se o cursor interno do array estiver apontando para além do final do array, <B CLASS="function" >each()</B > retorna <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="AEN12458" ></A ><P ><B >Exemplo 1. Exemplos de <B CLASS="function" >each()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br />$foo </font><font color="#007700">= array(</font><font color="#DD0000">"bob"</font><font color="#007700">, </font><font color="#DD0000">"fred"</font><font color="#007700">, </font><font color="#DD0000">"jussi"</font><font color="#007700">, </font><font color="#DD0000">"jouni"</font><font color="#007700">, </font><font color="#DD0000">"egon"</font><font color="#007700">, </font><font color="#DD0000">"marliese"</font><font color="#007700">); <br /></font><font color="#0000BB">$bar </font><font color="#007700">= </font><font color="#0000BB">each</font><font color="#007700">(</font><font color="#0000BB">$foo</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$bar</font><font color="#007700">); <br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > <CODE CLASS="varname" >$bar</CODE > agora contém os seguintes pares de chaves e valores: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [1] => bob [value] => bob [0] => 0 [key] => 0 )</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN12466" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php <br /> $foo </font><font color="#007700">= array(</font><font color="#DD0000">"Robert" </font><font color="#007700">=> </font><font color="#DD0000">"Bob"</font><font color="#007700">, </font><font color="#DD0000">"Seppo" </font><font color="#007700">=> </font><font color="#DD0000">"Sepi"</font><font color="#007700">); <br /> </font><font color="#0000BB">$bar </font><font color="#007700">= </font><font color="#0000BB">each</font><font color="#007700">(</font><font color="#0000BB">$foo</font><font color="#007700">); <br /> </font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$bar</font><font color="#007700">); <br /> </font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > <CODE CLASS="varname" >$bar</CODE > agora contém os seguintes pares de chaves e valores: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Array ( [1] => Bob [value] => Bob [0] => Robert [key] => Robert )</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > <B CLASS="function" >each()</B > é tipicamente usada em conjunto com <A HREF="function.list.html" ><B CLASS="function" >list()</B ></A > para percorrer um array, por exemplo: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN12474" ></A ><P ><B >Exemplo 2. Percorrendo um array com <B CLASS="function" >each()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php <br />$fruit </font><font color="#007700">= array(</font><font color="#DD0000">'a' </font><font color="#007700">=> </font><font color="#DD0000">'apple'</font><font color="#007700">, </font><font color="#DD0000">'b' </font><font color="#007700">=> </font><font color="#DD0000">'banana'</font><font color="#007700">, </font><font color="#DD0000">'c' </font><font color="#007700">=> </font><font color="#DD0000">'cranberry'</font><font color="#007700">); <br /></font><font color="#0000BB">reset</font><font color="#007700">(</font><font color="#0000BB">$fruit</font><font color="#007700">); <br />while (list(</font><font color="#0000BB">$key</font><font color="#007700">, </font><font color="#0000BB">$val</font><font color="#007700">) = </font><font color="#0000BB">each</font><font color="#007700">(</font><font color="#0000BB">$fruit</font><font color="#007700">)) { <br /> echo </font><font color="#DD0000">"$key => $val</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">; <br />}<br /></font><font color="#FF8000">/* Saída: <br /> <br /> a => apple <br /> b => banana <br /> c => cranberry <br /> <br /> */ <br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Depois da execução de <B CLASS="function" >each()</B >, o cursor interno do array vai apontar para o próximo elemento do array, ou após o último elemento se ele chegar ao final do array. Você deve usar <A HREF="function.reset.html" ><B CLASS="function" >reset()</B ></A > se quiser percorrer o array novamente. </P ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Cuidado</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > Because assigning an array to another variable resets the original arrays pointer, our example above would cause an endless loop had we assigned <CODE CLASS="varname" >$fruit</CODE > to another variable inside the loop. </P ></TD ></TR ></TABLE ></DIV ><P > Veja também <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 > e <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" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.end.html" ACCESSKEY="N" >Próxima</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" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >end</TD ></TR ></TABLE ></DIV ></BODY ></HTML >