<HTML ><HEAD ><TITLE >foreach</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.44"><LINK REL="HOME" TITLE="PHP Manual" HREF="manual.html"><LINK REL="UP" TITLE="Control Structures" HREF="control-structures.html"><LINK REL="PREVIOUS" TITLE="for" HREF="control-structures.for.html"><LINK REL="NEXT" TITLE="break" HREF="control-structures.break.html"></HEAD ><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><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="control-structures.for.html" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 11. Control Structures</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="control-structures.break.html" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.foreach" ><TT CLASS="literal" >foreach</TT ></A ></H1 ><P > PHP4 (not PHP3) includes a <TT CLASS="literal" >foreach</TT > construct, much like perl and some other languages. This simply gives an easy way to iterate over arrays. There are two syntaxes; the second is a minor but useful extension of the first: <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 foreach(array_expression as $value) statement 3 foreach(array_expression as $key => $value) statement 4 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > The first form loops over the array given by <TT CLASS="literal" >array_expression</TT >. On each loop, the value of the current element is assigned to <TT CLASS="literal" >$value</TT > and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element). </P ><P > The second form does the same thing, except that the current element's key will be assigned to the variable <TT CLASS="literal" >$key</TT > on each loop. </P ><P > When <TT CLASS="literal" >foreach</TT > first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call <A HREF="function.reset.html" ><B CLASS="function" >reset()</B ></A > before a <TT CLASS="literal" >foreach</TT > loop. </P ><P > You may have noticed that the following are functionally identical: <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 reset ($arr); 3 while (list(, $value) = each ($arr)) { 4 echo "Value: $value<br>\n"; 5 } 6 7 foreach ($arr as $value) { 8 echo "Value: $value<br>\n"; 9 } 10 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > The following are also functionally identical: <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 reset ($arr); 3 while (list($key, $value) = each ($arr)) { 4 echo "Key: $key; Value: $value<br>\n"; 5 } 6 7 foreach ($arr as $key => $value) { 8 echo "Key: $key; Value: $value<br>\n"; 9 } 10 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > Some more examples to demonstrate usages: <DIV CLASS="informalexample" ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 /* foreach example 1: value only */ 3 4 $a = array (1, 2, 3, 17); 5 6 foreach ($a as $v) { 7 print "Current value of \$a: $v.\n"; 8 } 9 10 /* foreach example 2: value (with key printed for illustration) */ 11 12 $a = array (1, 2, 3, 17); 13 14 $i = 0; /* for illustrative purposes only */ 15 16 foreach($a as $v) { 17 print "\$a[$i] => $k.\n"; 18 } 19 20 /* foreach example 3: key and value */ 21 22 $a = array ( 23 "one" => 1, 24 "two" => 2, 25 "three" => 3, 26 "seventeen" => 17 27 ); 28 29 foreach($a as $k => $v) { 30 print "\$a[$k] => $v.\n"; 31 } 32 </PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="control-structures.for.html" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="manual.html" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="control-structures.break.html" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><TT CLASS="literal" >for</TT ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="control-structures.html" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="literal" >break</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >