PHPforeach array loops _ PHP Tutorial

Source: Internet
Author: User
Tags php foreach
PHPforeach array loops. The PHPforeach () syntax structure is used to traverse operations or output arrays. foreach () can only be used to traverse arrays or objects, when trying to use it for another data type or an uninitialized variable, the PHP foreach () syntax structure is used to traverse operations or output arrays. foreach () can only be used to traverse arrays or objects, an error occurs when you try to use it for another data type or an uninitialized variable.

Syntax:

Foreach (array as $ value) statement // Or: foreach (array as $ key => $ value) statement

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

Usually, loop access arrays use a for loop. for example:

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

However, if you use manual code to operate a large array, there is less code to use the foreach loop. the above code can be written as follows:

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

Next we will discuss several issues in foreach usage.

1. use of references in foreach.

Under normal circumstances, $ arr and $ value in foreach ($ arr as $ value) are copied and are not affected by the external, that is

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

But if $ arr is referenced, the situation is different. We use code to describe the problem.

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

This is because $ arr in the loop directly points to the original data, rather than copying a copy.

If $ value is referenced, and $ arr is not referenced, the result is the same. Similarly, $ value points to the original data rather than copy.

$ Arr = array (,); foreach ($ arr as & $ value) {$ arr = array (); echo $ value;} // The result is: 0

Another special case is that if $ arr is defined as a global variable, $ 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 twice, you must not write it like this.

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

This will lead to incorrect results of the second loop (probably a php bug ). It can be replaced by the following types:

//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){}$arr2 = $arr;foreach($arr2 as $value){}

3. prevent foreach from being undefined. write foreach as much as possible.

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

The http://www.bkjia.com/PHPjc/752450.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752450.htmlTechArticlePHP foreach () syntax structure is used to traverse operations or output arrays, and foreach () can only be used to traverse arrays or objects, when trying to use it for another data type or an uninitialized variable...

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.