Home > Article > Backend Development > How to traverse an array in php?
There are three commonly used methods to traverse arrays in PHP:
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 header("Content-Type: text/html; charset=utf-8"); $arr = array('https://www.php.cn','php中文网','PHP教程'); $num = count($arr); for($i=0;$i<$num;++$i){ echo $arr[$i].'<br />'; } ?>
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;$i< count($arr);++$i), each loop will calculate the number of elements in the array $arr, and the above method can be used to subtract this overhead. The use of ++$i is also to improve efficiency. We have mentioned it in previous articles and I suggest you read it again.
The output result of the above code is:
https://www.php.cn php中文网 PHP教程
2. Use the foreach statement to traverse the array
There are two ways to use the foreach statement to loop through the array There are two ways, and the one we use most is still the first way. The introduction is as follows:
The first way:
foreach(array_expression as $value){ //循环体 }
Example code:
<?php header("Content-Type: text/html; charset=utf-8"); $arr = array('https://www.php.cn','php中文网','PHP教程'); foreach($arr as $value){ echo $value.'<br />'; } ?>
In each loop, the value of the current element is assigned to the variable $value, and the array The internal pointer moves back one step. Therefore, the next element of the array will be obtained in the next loop, and the loop will not stop until the end of the array, ending the traversal of the array.
Second method:
foreach(array_expression as $key=>$value){ //循环体 }
Instance code:
<?php header("Content-Type: text/html; charset=utf-8"); $arr = array('https://www.php.cn','php中文网','PHP教程'); foreach($arr as $k=>$v){ echo $k."=>".$v."<br />"; } ?>
Output:
0=>https://www.php.cn 1=>php中文网 2=>PHP教程
3. Combined use list(), each() and while loops traverse the array
each() function needs to pass an array as a parameter, 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 header("Content-Type: text/html; charset=utf-8"); //定义循环的数组 $arr = array('website'=>'https://www.php.cn','webname'=>'php中文网'); while(list($k,$v) = each($arr)){ echo $k.'=>'.$v.'<br />'; } ?>
Output:
website=>https://www.php.cn webname=>php中文网
For more related knowledge, please visit PHP Chinese website! !
The above is the detailed content of How to traverse an array in php?. For more information, please follow other related articles on the PHP Chinese website!