PHP several ways to traverse groups

  • 2020-05-16 06:28:59
  • OfStack

There are three common methods for traversing groups in PHP:
1. Loop through groups using for statements;
2. Traversing groups with foreach statements;
3. Loop through groups using list(), each(), and while in combination.
The most efficient of these three methods is to traverse groups using the foreach statement. Since PHP4, foreach structure has been introduced, which is a statement designed for traversing groups in PHP. It is recommended to use. We will introduce these methods separately.

1. Loop through groups using the for statement
It is important to note that using the for statement to loop through an array requires that the array being traversed be an indexed array. PHP has not only associative arrays but also indexed arrays, so PHP rarely loops through groups with the for statement.
The example code is as follows:
 
<?php 
$arr = array('//www.ofstack.com',' The home of the script ','PHP The tutorial '); 
$num = count($arr); 
for($i=0;$i<$num;++$i){ 
echo $arr[$i].'<br />'; 
} 
?> 

Note: in the code above, we first calculated the number of elements in the array $arr before using it in the for statement, which is very efficient. Because if it's for($i=0; $i < count ($arr); ++$i), each loop counts the number of elements in the array $arr, and this overhead can be subtracted using the above method. The use of ++$i is also to improve efficiency, as we mentioned in the previous article. I suggest you take a look at it again.
The output of the above code is:
//www.ofstack.com
This site
PHP tutorial

2. Iterate over groups using the foreach statement
There are two ways to loop through groups using the foreach statement, and we use the first most often. The introduction is as follows:
Method 1:
foreach(array_expression as $value){
/ / the loop body
}
Example code:
 
<?php 
$arr = array('//www.ofstack.com',' The home of the script ','PHP The tutorial '); 
foreach($arr as $value){ 
echo $value.'<br />'; 
} 
?> 


In each loop, the value of the current element is assigned to the variable $value, and the pointer inside the array is moved back 1 step. So you get the next element of the array in the next loop, and you don't stop the loop until you get to the end of the array, and you end up traversing the array.

The second way:
foreach(array_expression as $key= > $value){
/ / the loop body
}
Example code:
 
<?php 
// Define an array  
$arr = array('//www.ofstack.com',' The home of the script ','PHP The tutorial '); 
foreach($arr as $k=>$v){ 
echo $k."=>".$v."<br />"; 
} 
?> 


3. Loop through groups using list(), each(), and while in combination
The each() function passes an array as a parameter, returns the key/value pair for the current element in the array, and moves the array pointer backward to the next element.
The list() function, which is not a real function, is a language construct of PHP. list() assigns values to a set of variables in a one-step operation.

Example code:
 
<?php 
// Defines an array of loops  
$arr = array('website'=>'//www.ofstack.com','webname'=>' The home of the script ') 
while(list($k,$v) = each($arr)){ 
echo $k.'=>'.$v.'<br />'; 
} 
?> ofstack.com 

The output result is:
website= > //www.ofstack.com
webname= > PHP programmers

Conclusion: among the three methods mentioned above, it is recommended to use foreach statement to iterate over groups, which is more efficient.

Related articles: