PHP traversal array method summary
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.
?
1 2 3 4 5 6 |
<? Php $ Urls = array ('aaa', 'bbb ', 'ccc', 'ddd '); Foreach ($ urls as $ url ){ Echo "This Site url is $ url! <Br/> "; } ?> |
Display result:
?
1 2 3 4 |
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.
?
1 2 3 4 5 6 |
<? Php $ Urls = array ('aaa', 'bbb ', 'ccc', 'ddd '); While (list ($ key, $ val) = each ($ urls )){ Echo "This Site url is $ val. <br/> "; } ?> |
Display result:
?
1 2 3 4 |
This Site url is aaa This Site url is bbb This Site url is ccc This Site url is ddd |
Third, for () uses for to traverse Arrays
?
1 2 3 4 5 6 7 |
<? Php $ Urls = array ('aaa', 'bbb ', 'ccc', 'ddd '); For ($ I = 0; $ I <count ($ urls); $ I ++ ){ $ Str = $ urls [$ I]; Echo "This Site url is $ str. <br/> "; } ?> |
Display result:
?
1 2 3 4 |
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.
===============Below to 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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<? Php $ Arr = array (); For ($ I = 0; I 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) <br/> '; 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) <br/> '; 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) <br/> '; ?> |
Test results:
?
1 2 3 |
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.