The foreach () syntax structure provides a simple way to iterate through an array

Source: Internet
Author: User

The foreach syntax structure provides an easy way to iterate through an array. foreach can only be applied to arrays and objects, if you try to apply a variable to another data type, or an uninitialized variable will emit an error message. There are two kinds of syntax:

foreach  as $value )    statementforeachas$key$value)    statement

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

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

Note:

When foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the foreach loop .

Because foreach relies on an internal array pointer, modifying its value in a loop can cause unexpected behavior.

You can easily modify the elements of an array by adding & to it before $value . This method assigns a value to a reference instead of copying a value.

<? PHP $arr Array (1, 2, 3, 4); foreach ($arr as &$value) {    $value$value * 2;} // $arr is now Array (2, 4, 6, 8) unset ($value//  finally cancel the reference ?>

A reference to a $value is available only if the array being traversed can be referenced (for example, a variable). The following code does not work:

<? PHP foreach (array as &$value) {    $value$value * 2;}? >

But after the actual run,

<? PHP foreach (array as &$value) {    $value$value * 2;     Echo $value. ' <br/> ';}

Output: 2 4 6 8

Remove & Quote Symbols

<? PHP foreach (arrayas$value) {    $value$value * 2;     Echo $value. ' <br/> ';}

Output: 2 4 6 8

Warning: the $value Reference of the last element of the array remains after the foreach loop. It is recommended to use unset () to destroy it.

Note: foreach does not support the ability to suppress error messages with "@".

The user may notice that the following code functions exactly the same:

<?PHP$arr=Array("One", "one", "three");Reset($arr); while(List(,$value) = each($arr)) {    Echo"Value:$value<br>\n ";}foreach($arr  as $value) {    Echo"Value:$value&LT;BR/>\n ";}?>

The following code functions are identical:

<?PHP$arr=Array("One", "one", "three");Reset($arr); while(List($key,$value) = each($arr)) {    Echo"Key:$key; Value:$value&LT;BR/>\n ";}foreach($arr  as $key=$value) {    Echo"Key:$key; Value:$value&LT;BR/>\n ";}?>

http://php.net/manual/zh/control-structures.foreach.php

The foreach () syntax structure provides a simple way to iterate through an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.