This article mainly introduces three methods and efficiency comparison of PHP traversal arrays. The example analyzes the methods and efficiency comparison of foreach, while, and for traversal arrays, which has some reference value, for more information about how to use PHP to traverse arrays, see the following example. Share it with you for your reference. The specific analysis is as follows:
Today, a friend asked me a question about how php traversed the array and told her a few. By the way, write an article to sum up. if the summary is incomplete, please point it out.
First, foreach ()
Foreach () is the simplest and most effective way to traverse data in an array.
<?php $urls= array('aaa','bbb','ccc','ddd'); foreach ($urls as $url){ echo "This Site url is $url!
"; } ?>
Display result:
This Site url is aaa This Site url is bbb This Site url is ccc This Site url is ddd
2. use while (), list (), and each () together.
<?php $urls= array('aaa','bbb','ccc','ddd'); while(list($key,$val)= each($urls)) { echo "This Site url is $val.
"; } ?>
Display result:
This Site url is aaaThis Site url is bbbThis Site url is cccThis Site url is ddd
Third, for () uses for to traverse arrays
<?php $urls= array('aaa','bbb','ccc','ddd'); for ($i= 0;$i< count($urls); $i++){ $str= $urls[$i]; echo "This Site url is $str.
"; } ?>
Display result:
This Site url is aaa This Site url is bbb This Site url is ccc This Site url is ddd
Sometimes some people are asking which of the following methods to traverse the array is faster. now, let's make a simple test to understand.
Next we will test the speed of the three traversal arrays.
Generally, there are three methods to traverse an array: for, while, and foreach. Among them, the easiest and most convenient is foreach. Next, let's test the time it takes to traverse a one-dimensional array with 50000 subtopics.
<?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:
Used time of for:0.0228429(s) Used time of while:0.0544658(s) Used time of foreach:0.0085628(s)
After repeated tests, the results show that for traversing the same array, foreach is the fastest, while is the slowest. In principle, foreach operates on the array copy (by copying the array), while moves the internal indicator of the array, while should be faster than foreach (because foreach first copies the array when starting execution, while directly moves the internal indicator .), But the result is just the opposite. The reason should be that foreach is implemented inside PHP, while is a general loop structure. Therefore, foreach is simple and efficient in common applications. In PHP5, foreach can also traverse class attributes.
I hope this article will help you with php programming.