language/control-structures/foreach.xml
e49940b757b35b8ef26bb64380c231eda7b49fc4
...
...
@@ -2,7 +2,7 @@
2
2
<!-- $Revision$ -->
3
3

4
4
<sect1 xml:id="control-structures.foreach" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
5
-
<title><literal>foreach</literal></title>
5
+
<title>foreach</title>
6
6
<?phpdoc print-version-for="foreach"?>
7
7
<para>
8
8
The <literal>foreach</literal> construct provides an easy way to
...
...
@@ -13,47 +13,33 @@
13
13
<informalexample>
14
14
<programlisting>
15
15
<![CDATA[
16
-
foreach (array_expression as $value)
16
+
foreach (iterable_expression as $value)
17
17
statement
18
-
foreach (array_expression as $key => $value)
18
+
foreach (iterable_expression as $key => $value)
19
19
statement
20
20
]]>
21
21
</programlisting>
22
22
</informalexample>
23
23
</para>
24
24
<simpara>
25
-
The first form loops over the array given by
26
-
<literal>array_expression</literal>. On each iteration, the value of
27
-
the current element is assigned to <literal>$value</literal> and
28
-
the internal array pointer is advanced by one (so on the next
29
-
iteration, you'll be looking at the next element).
25
+
The first form traverses the iterable given by
26
+
<literal>iterable_expression</literal>. On each iteration, the value of
27
+
the current element is assigned to <literal>$value</literal>.
30
28
</simpara>
31
29
<simpara>
32
30
The second form will additionally assign the current element's key to
33
31
the <literal>$key</literal> variable on each iteration.
34
32
</simpara>
35
33
<simpara>
34
+
Note that <literal>foreach</literal> does not modify the internal array
35
+
pointer, which is used by functions such as <function>current</function>
36
+
and <function>key</function>.
37
+
</simpara>
38
+
<simpara>
36
39
It is possible to
37
40
<link linkend="language.oop5.iterations">customize object iteration</link>.
38
41
</simpara>
39
-
<para>
40
-
<note>
41
-
<para>
42
-
In PHP 5, when <literal>foreach</literal> first starts executing, the
43
-
internal array pointer is automatically reset to the first element of the
44
-
array. This means that you do not need to call <function>reset</function>
45
-
before a <literal>foreach</literal> loop.
46
-
</para>
47
-
<para>
48
-
As <literal>foreach</literal> relies on the internal array pointer in PHP
49
-
5, changing it within the loop may lead to unexpected behavior.
50
-
</para>
51
-
<para>
52
-
In PHP 7, <literal>foreach</literal> does not use the internal array
53
-
pointer.
54
-
</para>
55
-
</note>
56
-
</para>
42
+

57
43
<para>
58
44
In order to be able to directly modify array elements within the loop precede
59
45
<literal>$value</literal> with &amp;. In that case the value will be assigned by
...
...
@@ -73,71 +59,50 @@ unset($value); // break the reference with the last element
73
59
</programlisting>
74
60
</informalexample>
75
61
</para>
76
-
<para>
77
-
Referencing <literal>$value</literal> is only possible if the iterated array can be
78
-
referenced (i.e. if it is a variable). The following code won't work:
79
-
<informalexample>
80
-
<programlisting role="php">
81
-
<![CDATA[
82
-
<?php
83
-
foreach (array(1, 2, 3, 4) as &$value) {
84
-
$value = $value * 2;
85
-
}
86
-
?>
87
-
]]>
88
-
</programlisting>
89
-
</informalexample>
90
-
</para>
91
62
<warning>
92
63
<para>
93
64
Reference of a <literal>$value</literal> and the last array element
94
65
remain even after the <literal>foreach</literal> loop. It is recommended
95
66
to destroy it by <function>unset</function>.
67
+
Otherwise you will experience the following behavior:
96
68
</para>
97
-
</warning>
98
-
<para>
99
-
<note>
100
-
<para>
101
-
<literal>foreach</literal> does not support the ability to
102
-
suppress error messages using '@'.
103
-
</para>
104
-
</note>
105
-
</para>
106
-
<para>
107
-
You may have noticed that the following are functionally
108
-
identical:
109
69
<informalexample>
110
70
<programlisting role="php">
111
71
<![CDATA[
112
72
<?php
113
-
$arr = array("one", "two", "three");
114
-
reset($arr);
115
-
while (list(, $value) = each($arr)) {
116
-
echo "Value: $value<br />\n";
73
+
$arr = array(1, 2, 3, 4);
74
+
foreach ($arr as &$value) {
75
+
$value = $value * 2;
117
76
}
77
+
// $arr is now array(2, 4, 6, 8)
78
+

79
+
// without an unset($value), $value is still a reference to the last item: $arr[3]
118
80

119
-
foreach ($arr as $value) {
120
-
echo "Value: $value<br />\n";
81
+
foreach ($arr as $key => $value) {
82
+
// $arr[3] will be updated with each value from $arr...
83
+
echo "{$key} => {$value} ";
84
+
print_r($arr);
121
85
}
86
+
// ...until ultimately the second-to-last value is copied onto the last value
87
+

88
+
// output:
89
+
// 0 => 2 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 2 )
90
+
// 1 => 4 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 4 )
91
+
// 2 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
92
+
// 3 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
122
93
?>
123
94
]]>
124
95
</programlisting>
125
96
</informalexample>
126
-
</para>
97
+
</warning>
127
98
<para>
128
-
The following are also functionally identical:
99
+
It is possible to iterate a constant array's value by reference:
129
100
<informalexample>
130
101
<programlisting role="php">
131
102
<![CDATA[
132
103
<?php
133
-
$arr = array("one", "two", "three");
134
-
reset($arr);
135
-
while (list($key, $value) = each($arr)) {
136
-
echo "Key: $key; Value: $value<br />\n";
137
-
}
138
-

139
-
foreach ($arr as $key => $value) {
140
-
echo "Key: $key; Value: $value<br />\n";
104
+
foreach (array(1, 2, 3, 4) as &$value) {
105
+
$value = $value * 2;
141
106
}
142
107
?>
143
108
]]>
...
...
@@ -145,6 +110,15 @@ foreach ($arr as $key => $value) {
145
110
</informalexample>
146
111
</para>
147
112
<para>
113
+
<note>
114
+
<para>
115
+
<literal>foreach</literal> does not support the ability to
116
+
suppress error messages using
117
+
<literal linkend="language.operators.errorcontrol">@</literal>.
118
+
</para>
119
+
</note>
120
+
</para>
121
+
<para>
148
122
Some more examples to demonstrate usage:
149
123
<informalexample>
150
124
<programlisting role="php">
...
...
@@ -211,7 +185,7 @@ foreach (array(1, 2, 3, 4, 5) as $v) {
211
185
<?phpdoc print-version-for="foreach.list"?>
212
186

213
187
<para>
214
-
PHP 5.5 added the ability to iterate over an array of arrays and unpack the
188
+
It is possible to iterate over an array of arrays and unpack the
215
189
nested array into loop variables by providing a <function>list</function>
216
190
as the value.
217
191
</para>
...
...
@@ -310,6 +284,7 @@ A: 3; B: 4; C:
310
284
</informalexample>
311
285
</para>
312
286
</sect2>
287
+

313
288
</sect1>
314
289

315
290
<!-- Keep this comment at the end of the file
316
291