Several methods of traversing arrays in PHP_PHP tutorial

WBOY
Release: 2016-07-21 15:19:49
Original
857 people have browsed it

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 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 specifically designed for traversing arrays in PHP and is recommended for everyone to use. 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:

Copy code The code is as follows:

$arr = array ('http://www.jb51.net','Script Home','PHP Tutorial');
$num = count($arr);
for($i=0;$i< $num;++$i){
echo $arr[$i].'
';
}
?>

Comments: 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;$iThe output result of the above code is:
http://www.jb51.net
Script Home
PHP Tutorial

2. Use the foreach statement to traverse the array
There are two ways to use the foreach statement to loop through an array. The first method is the one we use most. The introduction is as follows:
The first way:
foreach(array_expression as $value){
//Loop body
}
Example code:
Copy code The code is as follows:

$arr = array('http://www.jb51.net','Script Home' ,'PHP tutorial');
foreach($arr as $value){
echo $value.'
';
}
?>


In each loop, the value of the current element is assigned to the variable $value, and the pointer inside the array is moved 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 way:
foreach(array_expression as $key=>$value){
//Loop body
}
Example code:
Copy code The code is as follows:

//Define array
$arr = array ('http://www.jb51.net','Script Home','PHP Tutorial');
foreach($arr as $k=>$v){
echo $k." =>".$v."
";
}
?>


3. Combined use of list(), each() and while loops traverse the array
each() function needs to pass an array as a parameter, return the key/value pair of the current element in the array, and move 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:
Copy code The code is as follows:

// Array that defines the loop
$arr = array('website'=>'http://www.jb51.net','webname'=>'Script Home')
while(list($ k,$v) = each($arr)){
echo $k.'=>'.$v.'
';
}
?> jb51. net

The output result is:
website=>http://www.jb51.net
webname=>PHP programmer

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325223.htmlTechArticleThere 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 Traverse the array; 3. Use list(), each() and while loop to traverse...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template