Home  >  Article  >  Backend Development  >  Several ways to loop through arrays in PHP using statements and functions

Several ways to loop through arrays in PHP using statements and functions

伊谢尔伦
伊谢尔伦Original
2017-06-22 15:22:001477browse

Traverse an array in PHPThere are three commonly used methods:

1. Use the for statement to loop through the array;
2. Use the foreach statement to traverse the array;
3. Use list(), each() and while loop in combination to traverse the array.
The most efficient of these three methods is to use the foreach statement to traverse the array. The foreach structure has been introduced since PHP4. It is a statement specially designed for traversing arrays in PHP. It is recommended that everyone use it. Let’s first introduce these methods respectively.

1. Use the for statement to loop through an array
It is worth noting that using the for statement to loop through an array requires that the array traversed must be an index array. There are not only associative arrays but also index arrays in PHP, so the for statement is rarely used in PHP to loop through arrays.
The example code is as follows:

<?php 
$arr = array(&#39;http://www.php.cn&#39;,&#39;php中文网&#39;,&#39;PHP教程&#39;); 
$num = count($arr); 
for($i=0;$i<$num;++$i){ 
echo $arr[$i].&#39;<br />&#39;; 
} 
?>

Note: In the above example code, we first calculate the number of elements in the array $arr, and then use it in the for statement. This is very efficient. Because if it is for($i=0;$ibaad7ddffead920171ddd1203e0d5bbc$value){
//Loop body
}
Example code:

<?php 
//定义数组 
$arr = array(&#39;http://www.php.cn&#39;,&#39;php中文网&#39;,&#39;PHP教程&#39;); 
foreach($arr as $k=>$v){ 
echo $k."=>".$v."<br />"; 
} 
?>


##3. Combined use of list(), each() and while loop to traverse the array each() function needs to be passed Takes an array as an argument, returns the key/value pair of the current element in the array, and moves the array pointer backward to the position of the next element.
list() function, this is not a real function, it is a language structure of PHP. list() assigns values ​​to a set of variables in one step.
Example code:

<?php 
//定义循环的数组 
$arr = array(&#39;website&#39;=>&#39;http://www.php.cn&#39;,&#39;webname&#39;=>&#39;php中文网&#39;) 
while(list($k,$v) = each($arr)){ 
echo $k.&#39;=>&#39;.$v.&#39;<br />&#39;; 
} 
?>

The output result is:

website=>http://www.php.cn
webname=>php Chinese website
Summary: the above three Among the methods of looping through an array, it is recommended that you use the foreach statement to loop through the array, which is more efficient.

The above is the detailed content of Several ways to loop through arrays in PHP using statements and functions. For more information, please follow other related articles on the PHP Chinese website!

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