Some problems with the PHP foreach array loop _php tutorial

Source: Internet
Author: User
Tags php foreach
The PHP foreach () syntax structure is used to traverse an operation or an output array, and foreach () can only be used to iterate over an array or an object, resulting in an error when trying to use it for another data type or an uninitialized variable.

Grammar:

foreach (array as $value)    statement//or: foreach (array as $key = = $value)    statement

In the above syntax, each loop assigns the value of the current cell to the $value and the pointer inside the array moves forward one step. In the second syntax format, the key name of the current cell is also assigned to the variable $key in each loop.

Typically iterating through an array is used for loops, for example:

for ($i = 0; $i < 3; $i + +) {echo $arr [$i];}

But by manipulating a large array with manual code, the code for the Foreach loop is less, and the code above can write:

foreach ($arr as $value) {echo $value;}

Here's a look at some of the problems in using foreach.

The case of using references in 1.foreach.

In general, $arr and $value in foreach ($arr as $value) are copies that are not externally affected, i.e.

$arr = Array (0,1,2,3,4,5), foreach ($arr as $value) {$arr = array (); Echo $value;} 12345

But if $arr is quoted, the situation is different, and we use the code to illustrate the problem

$arr = Array (0,1,2,3,4,5), $arr = & $arr, foreach ($arr as $value) {$arr = array (); Echo $value;} 0

This is because the $arr of the loop is directed directly to the original data, not a copy.

If $value is a reference, and $arr is not a reference, the result is the same, and the same $value is pointing to the original data instead of copy.

$arr = Array (0,1,2,3,4,5), foreach ($arr as & $value) {$arr = array (); Echo $value;} The result is: 0

One more special case is that if you define $arr as a global variable, $arr will become a reference:

Global $arr; $arr = Array (0,1,2,3,4,5), foreach ($arr as $value) {$arr = array (); Echo $value;} The result is: 0

2. If two loops an array, it must not be written like this

foreach ($arr as & $value) {}foreach ($arr as $value) {}

This causes the result of the second loop to be incorrect (possibly a PHP bug). Can be replaced by the following:

Solution 1foreach ($arr as & $value) {}unset ($value), foreach ($arr as $value) {}//solution 2foreach ($arr as &$ Value) {}foreach ($arr as & $value) {}//solution 3foreach ($arr as & $value) {} $arr 2 = $arr; foreach ($arr 2 as $value) {}

3. To prevent a foreach from being undefined, write the foreach as much as possible

foreach ((array) $arr as $value) {}

http://www.bkjia.com/PHPjc/752450.html www.bkjia.com true http://www.bkjia.com/PHPjc/752450.html techarticle the PHP foreach () syntax structure is used to traverse an operation or an output array, and foreach () can only be used to iterate over an array or object when trying to use it for another data type or an uninitialized variable ...

  • Related Article

    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.