Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-release > by-pkgid > ebb1914cf182a88528b4547490db1dd8 > files > 411

kdewebdev-quanta-doc-3.5.9-2mdv2008.1.x86_64.rpm

<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
>&#13;    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&nbsp;
  2&nbsp;foreach(array_expression as $value) statement
  3&nbsp;foreach(array_expression as $key =&#62; $value) statement
  4&nbsp;     </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    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
>&#13;    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
>&#13;    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
>&#13;    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&nbsp;
  2&nbsp;reset ($arr);
  3&nbsp;while (list(, $value) = each ($arr)) {
  4&nbsp;    echo "Value: $value&#60;br&#62;\n";
  5&nbsp;}
  6&nbsp;
  7&nbsp;foreach ($arr as $value) {
  8&nbsp;    echo "Value: $value&#60;br&#62;\n";
  9&nbsp;}
 10&nbsp;     </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&nbsp;
  2&nbsp;reset ($arr);
  3&nbsp;while (list($key, $value) = each ($arr)) {
  4&nbsp;    echo "Key: $key; Value: $value&#60;br&#62;\n";
  5&nbsp;}
  6&nbsp;
  7&nbsp;foreach ($arr as $key =&#62; $value) {
  8&nbsp;    echo "Key: $key; Value: $value&#60;br&#62;\n";
  9&nbsp;}
 10&nbsp;     </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    Some more examples to demonstrate usages:
    <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;/* foreach example 1: value only */
  3&nbsp;
  4&nbsp;$a = array (1, 2, 3, 17);
  5&nbsp;
  6&nbsp;foreach ($a as $v) {
  7&nbsp;   print "Current value of \$a: $v.\n";
  8&nbsp;}
  9&nbsp;
 10&nbsp;/* foreach example 2: value (with key printed for illustration) */
 11&nbsp;
 12&nbsp;$a = array (1, 2, 3, 17);
 13&nbsp;
 14&nbsp;$i = 0; /* for illustrative purposes only */
 15&nbsp;
 16&nbsp;foreach($a as $v) {
 17&nbsp;    print "\$a[$i] =&#62; $k.\n";
 18&nbsp;}
 19&nbsp;
 20&nbsp;/* foreach example 3: key and value */
 21&nbsp;
 22&nbsp;$a = array (
 23&nbsp;    "one" =&#62; 1,
 24&nbsp;    "two" =&#62; 2,
 25&nbsp;    "three" =&#62; 3,
 26&nbsp;    "seventeen" =&#62; 17
 27&nbsp;);
 28&nbsp;
 29&nbsp;foreach($a as $k =&#62; $v) {
 30&nbsp;    print "\$a[$k] =&#62; $v.\n";
 31&nbsp;}
 32&nbsp;     </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
>