Among the three methods, the most efficient is to use the foreach statement to traverse the array. The foreach structure has been introduced since PHP4. It is a PHP clause designed to traverse arrays. We recommend that you use it. These methods are introduced first.
Among the three methods, the most efficient is to use the foreach statement to traverse the array. The foreach structure has been introduced since PHP4. It is a PHP clause designed to traverse arrays. We recommend that you use it. These methods are introduced first.
There are three common methods to traverse arrays in PHP:
1. Use the for statement to traverse the array cyclically;
2. Use the foreach statement to traverse the array;
3. Use list (), each (), and while loop to traverse the array.
Among the three methods, the most efficient is to use the foreach statement to traverse the array. The foreach structure has been introduced since PHP4. It is a PHP clause designed to traverse arrays. We recommend that you use it. These methods are described first.
I. Use the for statement to traverse arrays cyclically
It is worth noting that using the for statement to traverse arrays cyclically requires that the arrays to be traversed must be indexed arrays. PHP contains not only associated arrays but also index arrays. Therefore, PHP seldom uses the for statement to traverse arrays cyclically.
The instance code is as follows:
The Code is as follows:
$ Arr = array ('HTTP: // www.jb51.net', 'foot home', 'php ');
$ Num = count ($ arr );
For ($ I = 0; $ I <$ num; ++ $ I ){
Echo $ arr [$ I].'
';
}
?>
Note: In the code above, 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), the number of elements in the array $ arr is calculated in each loop, the preceding method can be used to subtract the overhead. The usage of ++ $ I is also used to improve efficiency. We have mentioned this in the previous article. We suggest you try again.
The output result of the above Code is:
Http://www.jb51.net
Foot home
PHP tutorial
Ii. Use the foreach statement to traverse the Array
There are two ways to use the foreach statement to traverse arrays cyclically. The most commonly used method is the first one. Introduction:
Method 1:
Foreach (array_expression as $ value ){
// Loop body
}
Instance code:
The Code is as follows:
$ Arr = array ('HTTP: // www.jb51.net', 'foot home', 'php ');
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 one step backward. Therefore, the next element of the array is obtained in the next loop, and the loop is stopped until the end of the array.
Method 2:
Foreach (array_expression as $ key => $ value ){
// Loop body
}
Instance code:
The Code is as follows:
// Define an array
$ Arr = array ('HTTP: // www.jb51.net', 'foot home', 'php ');
Foreach ($ arr as $ k =>$ v ){
Echo $ k. "=>". $ v ."
";
}
?>
3. Use list (), each (), and while loop to traverse the Array
The 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 to the next element.
List () function. This is not a real function. It is a language structure of PHP. List () assigns values to a group of variables with one-step operations.
Instance code:
The Code is as follows:
// Define the array of loops
$ Arr = array ('website' => 'HTTP: // www.jb51.net', 'webname' => 'feet home ')
While (list ($ k, $ v) = each ($ arr )){
Echo $ k. '=>'. $ v .'
';
}
?> Jb51.net
Output result:
Website => http://www.jb51.net
Webname => PHP programmer
Conclusion: We recommend that you use the foreach statement to traverse arrays cyclically in the preceding three methods to improve efficiency.