PHP iterate through the array of methods Summary, PHP all over the summary
A friend of mine asked me a question today. PHP iterates through the array, telling her a few. By the way, write a summary of the article, if the summary is not complete also ask friends to point out
First, foreach ()
foreach () is the simplest and most efficient way to iterate through the data in an array.
<?php $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd '); foreach ($urls as $url) {
";
Show Results:
Second, while () and list (), each () is used together.
<?php $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd '); while (list ($key, $val) = each ($urls)) { echo ' This Site URL is $val.
";
Show Results:
Third, for () use for traversal array
<?php $urls = Array (' AAA ', ' BBB ', ' CCC ', ' ddd '); for ($i = 0; $i < count ($urls), $i + +) { $str = $urls [$i]; echo "This Site URL is $str.
";
Show Results:
Sometimes someone is asking these kinds of ways to iterate over an array which is quicker, and the following is a simple test to understand
=========== below to test the speed of three traversal arrays ===========
In general, there are three ways to traverse an array, for, while, foreach. One of the simplest and most convenient is foreach. Let's test the time spent together traversing a one-dimensional array with 50,000 subscripts.
<?php $arr = Array (); for ($i = 0; $i < 50000; $i + +) {$arr []= $i *rand (1000,9999); } function GetRuntime () {list ($usec, $sec) =explode ("", Microtime ()); return (float) $usec + (float) $sec); } ###################################### $time _start= getruntime (); for ($i = 0; $i < count ($arr), $i + +) {$str = $arr [$i]; } $time _end= getruntime (); $time _used= $time _end-$time _start; Echo ' used time of For: '. Round ($time _used, 7). ' (s)
'; Unset ($str, $time _start, $time _end, $time _used); ###################################### $time _start= getruntime (); while (list ($key, $val) = each ($arr)) {$str = $val; } $time _end= getruntime (); $time _used= $time _end-$time _start; Echo ' used time of while: '. Round ($time _used, 7). ' (s)
'; Unset ($str, $key, $val, $time _start, $time _end, $time _used); ###################################### $time _start= getruntime (); foreach ($arr as$key=> $val) {$str = $val; } $time _end= getruntime (); $time _used= $time _end-$time _start; Echo ' Used time of foreach: '. Round ($time _used, 7). ' (s)
'; ?>
Test results:
After repeated tests, the result shows that for traversing the same array, the foreach speed is the fastest and the slowest is the while. In principle, foreach operates on an array copy (by copying the arrays), while the while is manipulated by moving the internal indicators of the array, which is generally considered to be faster than foreach (since foreach first copies the array in the beginning of execution). While the while moves the internal indicator directly. ), but the result is just the opposite. The reason should be that foreach is an internal implementation of PHP, while the while is a common loop structure. Therefore, foreach is simple and efficient in common applications. Under PHP5, foreach can also traverse the properties of a class.
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/992546.html www.bkjia.com true http://www.bkjia.com/PHPjc/992546.html techarticle PHP iterate through the array of methods Summary, PHP all over the summary today, a friend asked me a question PHP traversal array method, told her a few. By the way write a summary of the article, if the summary is not complete ...