notes and examples added to foreach

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@45084 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Daniel Beckham 2001-04-09 21:50:14 +00:00
parent 91809f04d3
commit aeab65b087

View file

@ -461,23 +461,31 @@ foreach(array_expression as $key => $value) statement
<para>
<note>
<para>
When <literal>foreach</literal> first starts executing, the
When <literal>foreach</literal> 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
<function>reset</function> before a <literal>foreach</literal>
loop.
</para>
</note>
</para>
</note>
</para>
<para>
<note>
<para>
Also note that <literal>foreach</literal> operates on a copy of
the specified array, not the array itself, therefore the array
pointer is not modified like with the each construct.
</para>
<para>
Also note that <literal>foreach</literal> operates on a copy of
the specified array, not the array itself, therefore the array
pointer is not modified as with the <function>each</function>
construct and changes to the array element returned are not
reflected in the original array.
</para>
</note>
</para>
<note>
<para>
<literal>foreach</literal> does not support the ability to
suppress error messages using '@'.
</para>
</note>
<para>
You may have noticed that the following are functionally
identical:
@ -541,6 +549,25 @@ $a = array (
foreach($a as $k =&gt; $v) {
print "\$a[$k] =&gt; $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";
}
</programlisting>
</informalexample>
</para>