<HTML ><HEAD ><TITLE >foreach</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="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"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" 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="control-structures.for.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 12. Control Structures</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="control-structures.break.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.foreach" ></A ><TT CLASS="literal" >foreach</TT ></H1 ><P > PHP 4 (not PHP 3) 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. <TT CLASS="literal" >foreach</TT > works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variables. There are two syntaxes; the second is a minor but useful extension of the first: <DIV CLASS="informalexample" ><A NAME="AEN4961" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >foreach(array_expression as $value) statement foreach(array_expression as $key => $value) statement</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 > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > 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 ></BLOCKQUOTE ></DIV > </P ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Also note that <TT CLASS="literal" >foreach</TT > operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the <A HREF="function.each.html" ><B CLASS="function" >each()</B ></A > construct and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array <SPAN CLASS="emphasis" ><I CLASS="emphasis" >is</I ></SPAN > advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array. </P ></BLOCKQUOTE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > <TT CLASS="literal" >foreach</TT > does not support the ability to suppress error messages using '@'. </P ></BLOCKQUOTE ></DIV ><P > You may have noticed that the following are functionally identical: <DIV CLASS="informalexample" ><A NAME="AEN4984" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >reset ($arr); while (list(, $value) = each ($arr)) { echo "Value: $value<br>\n"; } foreach ($arr as $value) { echo "Value: $value<br>\n"; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > The following are also functionally identical: <DIV CLASS="informalexample" ><A NAME="AEN4986" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >reset ($arr); while (list($key, $value) = each ($arr)) { echo "Key: $key; Value: $value<br>\n"; } foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br>\n"; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > Some more examples to demonstrate usages: <DIV CLASS="informalexample" ><A NAME="AEN4989" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >/* foreach example 1: value only */ $a = array (1, 2, 3, 17); foreach ($a as $v) { print "Current value of \$a: $v.\n"; } /* foreach example 2: value (with key printed for illustration) */ $a = array (1, 2, 3, 17); $i = 0; /* for illustrative purposes only */ foreach($a as $v) { print "\$a[$i] => $v.\n"; $i++; } /* foreach example 3: key and value */ $a = array ( "one" => 1, "two" => 2, "three" => 3, "seventeen" => 17 ); foreach($a as $k => $v) { print "\$a[$k] => $v.\n"; } /* foreach example 4: multi-dimensional arrays */ $a[0][0] = "a"; $a[0][1] = "b"; $a[1][0] = "y"; $a[1][1] = "z"; foreach($a as $v1) { foreach ($v1 as $v2) { print "$v2\n"; } } /* foreach example 5: dynamic arrays */ foreach(array(1, 2, 3, 4, 5) as $v) { print "$v\n"; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </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="control-structures.for.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="control-structures.break.html" ACCESSKEY="N" >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" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="literal" >break</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >