Home  >  Article  >  Backend Development  >  Introduction to how to use php foreach to traverse arrays_PHP tutorial

Introduction to how to use php foreach to traverse arrays_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:16:151000browse

There are many ways to traverse arrays in php, such as foreach, each, list, for, etc. to traverse array statements. However, among these, foreach has the best performance in traversing arrays, so I also use it commonly. Let me introduce the beginner foreach. Iterate over array instances.


Grammar

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

Example 1:

The code is as follows Copy code
 代码如下 复制代码

$color=array('white' => '白色' ,
       'black' => '黑色' ,
       'red' => '红色' ,
       'green' => '绿色',
       'yellow' => '黄色');
 foreach( $color as $c) echo $c ."
";   
?>

$color=array('white' => 'white' ,

'black' => 'black' ,
 代码如下 复制代码

 

foreach( $color as $c) echo $c ."
";

foreach( $color as $key => $c) echo $key.$c ."
";

'red' => 'red' ,

'green' => 'green',
        'yellow' => 'yellow');

foreach( $color as $c) echo $c ."
";

?>

Not only the value of the element but also the key name can be obtained through foreach. The structural form:

The code is as follows Copy code
 代码如下 复制代码

$arr = array(0,1,2,3,4,5);

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

// 12345

foreach( $color as $c) echo $c ."
";
 代码如下 复制代码

$arr = array(0,1,2,3,4,5);
$arr = &$arr;

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

// 0

or

foreach( $color as $key => $c) echo $key.$c ."
";

 代码如下 复制代码


$arr = array(0,1,2,3,4,5);

foreach($arr as &$value){
 $arr = array();
 echo $value;
}

// 结果是: 0

The difference between this and the first method is that there is an additional $key, that is, in addition to assigning the value of the current element to $value, the key value of the current element will also be assigned to the variable $key in each loop. The key value can be a subscript value or a string. For example, "0" in book[0]=1, "id" in book[id]="001". Let’s discuss some issues in using foreach in detail. Under normal circumstances, $arr and $value in foreach($arr as $value) are copies and are not affected by external factors, that is,
The code is as follows Copy code
$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 illustrate the problem
The code is as follows Copy code
$arr = array(0,1,2,3,4,5); $arr = &$arr; foreach($arr as $value){ $arr = array(); echo $value; } // 0
This is because the $arr used in the loop points directly to the original data instead of copying it. If $value is a reference and $arr is not a reference, the result is the same. Similarly, $value points to the original data instead of copy.
The code is as follows Copy code
$arr = array(0,1,2,3,4,5); foreach($arr as &$value){ $arr = array(); echo $value; } // The result is: 0

There is another special situation, that is, if $arr is defined as a global variable, $arr will also become a reference:

The code is as follows
 代码如下 复制代码

global $arr;
$arr = array(0,1,2,3,4,5);

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

// 结果是: 0

Copy code

 代码如下 复制代码

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

global $arr;
$arr = array(0,1,2,3,4,5);

foreach($arr as $value){
$arr = array();
echo $value;
}
 代码如下 复制代码

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

// The result is: 0

 代码如下 复制代码

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

If you loop through an array twice, you must not write it like this
The code is as follows

Copy code
foreach($arr as &$value){}
foreach($arr as $value){} This will cause the result of the second loop to be incorrect (maybe it is a PHP bug). You can use the following instead
The code is as follows Copy code
//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){}
$arr2 = $arr;
foreach($arr2 as $value){}
To prevent foreach from being undefined, try to write foreach like this
The code is as follows Copy code
foreach((array)$arr as $value) {} http://www.bkjia.com/PHPjc/628708.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628708.htmlTechArticleThere are many ways to traverse arrays in php, such as foreach, each, list, for, etc. to traverse array statements, but in Among these, foreach has the best performance in traversing arrays, so I also use it frequently. I will introduce it below...
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