<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>foreach</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.for.html">for</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.break.html">break</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="control-structures.foreach" class="sect1"> <h2 class="title"><em>foreach</em></h2> <p class="verinfo">(PHP 4, PHP 5)</p> <p class="para"> The <em>foreach</em> construct provides an easy way to iterate over arrays. <em>foreach</em> works only on arrays and objects, 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: <div class="informalexample"> <div class="example-contents"> <div class="cdata"><pre> foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement </pre></div> </div> </div> </p> <p class="simpara"> The first form loops over the array given by <em>array_expression</em>. On each iteration, the value of the current element is assigned to <em>$value</em> and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element). </p> <p class="simpara"> The second form will additionally assign the current element's key to the <em>$key</em> variable on each iteration. </p> <p class="simpara"> It is possible to <a href="language.oop5.iterations.html" class="link">customize object iteration</a>. </p> <p class="para"> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> When <em>foreach</em> 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 <span class="function"><a href="function.reset.html" class="function">reset()</a></span> before a <em>foreach</em> loop. </p> <p class="para"> As <em>foreach</em> relies on the internal array pointer, changing it within the loop may lead to unexpected behavior. </p> </p></blockquote> </p> <p class="para"> In order to be able to directly modify array elements within the loop precede <em>$value</em> with &. In that case the value will be assigned by <a href="language.references.html" class="link">reference</a>. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">);<br />foreach (</span><span style="color: #0000BB">$arr </span><span style="color: #007700">as &</span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">$value </span><span style="color: #007700">* </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />}<br /></span><span style="color: #FF8000">// $arr is now array(2, 4, 6, 8)<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$value</span><span style="color: #007700">); </span><span style="color: #FF8000">// break the reference with the last element<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> Referencing <em>$value</em> is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code won't work: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">foreach (array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">) as &</span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">$value </span><span style="color: #007700">* </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> Reference of a <em>$value</em> and the last array element remain even after the <em>foreach</em> loop. It is recommended to destroy it by <span class="function"><a href="function.unset.html" class="function">unset()</a></span>. </p> </div> <p class="para"> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> <em>foreach</em> does not support the ability to suppress error messages using '@'. </p> </p></blockquote> </p> <p class="para"> You may have noticed that the following are functionally identical: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">, </span><span style="color: #DD0000">"two"</span><span style="color: #007700">, </span><span style="color: #DD0000">"three"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">reset</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">);<br />while (list(, </span><span style="color: #0000BB">$value</span><span style="color: #007700">) = </span><span style="color: #0000BB">each</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"Value: </span><span style="color: #0000BB">$value</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br />}<br /><br />foreach (</span><span style="color: #0000BB">$arr </span><span style="color: #007700">as </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Value: </span><span style="color: #0000BB">$value</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> The following are also functionally identical: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">, </span><span style="color: #DD0000">"two"</span><span style="color: #007700">, </span><span style="color: #DD0000">"three"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">reset</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">);<br />while (list(</span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">) = </span><span style="color: #0000BB">each</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"Key: </span><span style="color: #0000BB">$key</span><span style="color: #DD0000">; Value: </span><span style="color: #0000BB">$value</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br />}<br /><br />foreach (</span><span style="color: #0000BB">$arr </span><span style="color: #007700">as </span><span style="color: #0000BB">$key </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Key: </span><span style="color: #0000BB">$key</span><span style="color: #DD0000">; Value: </span><span style="color: #0000BB">$value</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> Some more examples to demonstrate usage: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* foreach example 1: value only */<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">17</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$a </span><span style="color: #007700">as </span><span style="color: #0000BB">$v</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Current value of \$a: </span><span style="color: #0000BB">$v</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/* foreach example 2: value (with its manual access notation printed for illustration) */<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">17</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #FF8000">/* for illustrative purposes only */<br /><br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$a </span><span style="color: #007700">as </span><span style="color: #0000BB">$v</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"\$a[</span><span style="color: #0000BB">$i</span><span style="color: #DD0000">] => </span><span style="color: #0000BB">$v</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$i</span><span style="color: #007700">++;<br />}<br /><br /></span><span style="color: #FF8000">/* foreach example 3: key and value */<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">"one" </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"two" </span><span style="color: #007700">=> </span><span style="color: #0000BB">2</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"three" </span><span style="color: #007700">=> </span><span style="color: #0000BB">3</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"seventeen" </span><span style="color: #007700">=> </span><span style="color: #0000BB">17<br /></span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$a </span><span style="color: #007700">as </span><span style="color: #0000BB">$k </span><span style="color: #007700">=> </span><span style="color: #0000BB">$v</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"\$a[</span><span style="color: #0000BB">$k</span><span style="color: #DD0000">] => </span><span style="color: #0000BB">$v</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/* foreach example 4: multi-dimensional arrays */<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array();<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">] = </span><span style="color: #DD0000">"a"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #0000BB">1</span><span style="color: #007700">] = </span><span style="color: #DD0000">"b"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">] = </span><span style="color: #DD0000">"y"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">][</span><span style="color: #0000BB">1</span><span style="color: #007700">] = </span><span style="color: #DD0000">"z"</span><span style="color: #007700">;<br /><br />foreach (</span><span style="color: #0000BB">$a </span><span style="color: #007700">as </span><span style="color: #0000BB">$v1</span><span style="color: #007700">) {<br /> foreach (</span><span style="color: #0000BB">$v1 </span><span style="color: #007700">as </span><span style="color: #0000BB">$v2</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$v2</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #FF8000">/* foreach example 5: dynamic arrays */<br /><br /></span><span style="color: #007700">foreach (array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">) as </span><span style="color: #0000BB">$v</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$v</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <div class="sect2" id="control-structures.foreach.list"> <h3 class="title">Unpacking nested arrays with list()</h3> <p class="verinfo">(PHP 5 >= 5.5.0)</p> <p class="para"> PHP 5.5 added the ability to iterate over an array of arrays and unpack the nested array into loop variables by providing a <span class="function"><a href="function.list.html" class="function">list()</a></span> as the value. </p> <p class="para"> For example: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$array </span><span style="color: #007700">= [<br /> [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">],<br /> [</span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">],<br />];<br /><br />foreach (</span><span style="color: #0000BB">$array </span><span style="color: #007700">as list(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// $a contains the first element of the nested array,<br /> // and $b contains the second element.<br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">"A: </span><span style="color: #0000BB">$a</span><span style="color: #DD0000">; B: </span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> A: 1; B: 2 A: 3; B: 4 </pre></div> </div> </div> </p> <p class="para"> You can provide fewer elements in the <span class="function"><a href="function.list.html" class="function">list()</a></span> than there are in the nested array, in which case the leftover array values will be ignored: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$array </span><span style="color: #007700">= [<br /> [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">],<br /> [</span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">],<br />];<br /><br />foreach (</span><span style="color: #0000BB">$array </span><span style="color: #007700">as list(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// Note that there is no $b here.<br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> 1 3 </pre></div> </div> </div> </p> <p class="para"> A notice will be generated if there aren't enough array elements to fill the <span class="function"><a href="function.list.html" class="function">list()</a></span>: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$array </span><span style="color: #007700">= [<br /> [</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">],<br /> [</span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">],<br />];<br /><br />foreach (</span><span style="color: #0000BB">$array </span><span style="color: #007700">as list(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">$c</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"A: </span><span style="color: #0000BB">$a</span><span style="color: #DD0000">; B: </span><span style="color: #0000BB">$b</span><span style="color: #DD0000">; C: </span><span style="color: #0000BB">$c</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> Notice: Undefined offset: 2 in example.php on line 7 A: 1; B: 2; C: Notice: Undefined offset: 2 in example.php on line 7 A: 3; B: 4; C: </pre></div> </div> </div> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.for.html">for</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.break.html">break</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>