PHP foreach Array Loop FAQ

Source: Internet
Author: User
Tags foreach php foreach reference
The <?php/*php foreach () syntax structure is used to traverse an operation or output array, and foreach () can only be used to traverse an array or an object, and an error occurs when an attempt is made to use it for another data type or an uninitialized variable. Syntax: */foreach (array as $value) statement//or: foreach (array as $key => $value) Statement/* Above syntax, each loop will be The value of the front cell is assigned to the $value and the pointer inside the array moves forward one step. 
The key name of the current cell is also assigned to the variable $key in each loop in the second syntax format. Url:http://www.bianceng.cn/webkf/php/201410/45950.htm usually iterates through the array in a for loop, for example: */for ($i = 0; $i < 3; $i + +) {Ech  
o $arr [$i];  
//But with a large array of manual code, there is less code with a foreach loop, and the above code can write: foreach ($arr as $value) {echo $value;  
       
//below to explore some of the issues in the next foreach usage.  
The use of references in 1.foreach.  
       
In general, $arr and $value in foreach ($arr as $value) are copies that are unaffected by the outside, i.e. $arr = Array (0,1,2,3,4,5);  
foreach ($arr as $value) {$arr = array ();  
Echo $value;  
//12345//But if $arr is a reference, the situation is different, we use code to explain 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 to the original data, not copy. If the $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 rather than copy.  
* * $arr = array (0,1,2,3,4,5);  
foreach ($arr as & $value) {$arr = array ();  
Echo $value;  
The result is: 0//There is a special situation, that is, if the $arr is defined as a global variable, the $arr will also 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 you loop an array two times, you must not write foreach ($arr as & $value) {} foreach ($arr as $value) {}///This will cause the result of the second loop to be incorrect.  
You can use the following to replace: View Sourceprint?  
Solution 1 foreach ($arr as & $value) {} unset ($value);  
foreach ($arr as $value) {}//solution 2 foreach ($arr as & $value) {} foreach ($arr as & $value) {}//solution 3  
foreach ($arr as & $value) {} $arr 2 = $arr; foreach ($arr 2 as $value) {}//3. To prevent a foreach from appearing undefined, try to write a foreach foreach (array) $arr as $value) {}. ;

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.