Home  >  Article  >  Backend Development  >  PHP foreach() function is limited to array use_PHP tutorial

PHP foreach() function is limited to array use_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:34:09802browse

This is just a convenient way to iterate over an array. The PHP foreach() function can only be used with arrays, and an error will occur when trying to use it with other data types or an uninitialized variable. There are two syntaxes, the second being a less important but useful extension of the first.

foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement

The first format iterates over the given array_expression array. Each time through the loop, the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step (so the next cell will be obtained in the next loop).

The second format does the same thing, except that the key name of the current cell is also assigned to the variable $key in each loop.

Since PHP 5, it is also possible to traverse objects.

Note: When the PHP foreach() function starts executing, the pointer inside the array will automatically point to the first unit. This means there is no need to call reset() before the foreach loop.

Note: Unless the array is referenced, the PHP foreach() function operates on a copy of the specified array, not the array itself. Therefore, the array pointer will not be changed by the each() structure, and modifications to the returned array cells will not affect the original array. However, the internal pointer of the original array does move forward during the processing of the array. Assuming that the foreach loop runs to the end, the internal pointer of the original array will point to the end of the array.

Since PHP 5, it is easy to modify the cells of an array by preceding $value with &. This method assigns by reference rather than copying a value.

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php   </span></span></li><li><span>$</span><span class="attribute">arr</span><span> = </span><span class="attribute-value">array</span><span>(1, 2, 3, 4);   </span></li><li class="alt"><span>foreach ($arr as &$value) {   </span></li><li><span>$</span><span class="attribute">value</span><span> = $value * 2;   </span></li><li class="alt"><span>}   </span></li><li><span>// $arr is now array(2, 4, 6, 8)   </span></li><li class="alt"><span class="tag">?></span><span>  </span></span></li></ol>

This method of the PHP foreach() function is only available when the array being traversed can be referenced (for example, it is a variable).


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446012.htmlTechArticleThis is just a convenient way to traverse an array. The PHP foreach() function can only be used with arrays, and an error will occur when trying to use it with other data types or an uninitialized variable. There are two kinds...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn