File: foreach.xml

package info (click to toggle)
php-doc 20100521-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 59,992 kB
  • ctags: 4,085
  • sloc: xml: 796,833; php: 21,338; cpp: 500; sh: 117; makefile: 58; awk: 28
file content (230 lines) | stat: -rw-r--r-- 5,665 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 288721 $ -->

<sect1 xml:id="control-structures.foreach" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <title><literal>foreach</literal></title>
 <para>
  PHP 4 introduced a <literal>foreach</literal> construct, much
  like Perl and some other languages. This simply gives an easy way to
  iterate over arrays. <literal>foreach</literal> 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 variable. There are two syntaxes; the
  second is a minor but useful extension of the first:
  <informalexample>
   <programlisting>
<![CDATA[
foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement
]]>
   </programlisting>
  </informalexample>
 </para>
 <simpara>
  The first form loops over the array given by
  <literal>array_expression</literal>. On each loop, the value of
  the current element is assigned to <literal>$value</literal> and
  the internal array pointer is advanced by one (so on the next
  loop, you'll be looking at the next element).
 </simpara>
 <simpara>
  The second form does the same thing, except that the current
  element's key will be assigned to the variable
  <literal>$key</literal> on each loop.
 </simpara>
 <simpara>
  As of PHP 5, it is possible to
  <link linkend="language.oop5.iterations">iterate objects</link> too.
 </simpara>
 <para>
  <note>
   <para>
    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>
 <para>
  <note>
   <para>
    Unless the array is <link linkend="language.references">referenced</link>,
    <literal>foreach</literal> operates on a copy of
    the specified array and not the array itself. <literal>foreach</literal>
    has some side effects on the array pointer. Don't rely on the array
    pointer during or after the foreach without resetting it.
   </para>
  </note>
 </para>
 <para>
  As of PHP 5, you can easily modify array's elements by preceding
  <literal>$value</literal> with &amp;. This will assign
  <link linkend="language.references">reference</link> instead of copying
  the value.
  <informalexample>
   <programlisting role="php">
<![CDATA[
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
]]>
   </programlisting>
  </informalexample>
  This is possible only if iterated array can be referenced (i.e. is
  variable), that means the following code won't work:
   <programlisting role="php">
<![CDATA[
<?php
foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
}

?>
]]>
   </programlisting>
 </para>
 <warning>
  <para>
   Reference of a <literal>$value</literal> and the last array element
   remain even after the <literal>foreach</literal> loop. It is recommended
   to destroy it by <function>unset</function>.
  </para>
 </warning>
 <para>
  <note>
   <para>
    <literal>foreach</literal> does not support the ability to
    suppress error messages using '@'.
   </para>
  </note>
 </para>
 <para>
  You may have noticed that the following are functionally
  identical:
  <informalexample>
   <programlisting role="php">
<![CDATA[
<?php
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
    echo "Value: $value<br />\n";
}

foreach ($arr as $value) {
    echo "Value: $value<br />\n";
}
?>
]]>
   </programlisting>
  </informalexample>
  The following are also functionally identical:
  <informalexample>
   <programlisting role="php">
<![CDATA[
<?php
$arr = array("one", "two", "three");
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";
}
?>
]]>
   </programlisting>
  </informalexample>
 </para>
 <para>
  Some more examples to demonstrate usages:
  <informalexample>
   <programlisting role="php">
<![CDATA[
<?php
/* foreach example 1: value only */

$a = array(1, 2, 3, 17);

foreach ($a as $v) {
    echo "Current value of \$a: $v.\n";
}

/* foreach example 2: value (with its manual access notation printed for illustration) */

$a = array(1, 2, 3, 17);

$i = 0; /* for illustrative purposes only */

foreach ($a as $v) {
    echo "\$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) {
    echo "\$a[$k] => $v.\n";
}

/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

/* foreach example 5: dynamic arrays */

foreach (array(1, 2, 3, 4, 5) as $v) {
    echo "$v\n";
}
?>
]]>
   </programlisting>
  </informalexample>
 </para>
</sect1>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->