PHP is still more commonly used, so I studied the PHP array traversal, here to take out and share with you, hope to be useful to everyone. About PHP array traversal, in fact, a lot of people have been tested, like http://www.lilov.org/post/18.html, but these discussions are actually too simple, here I will be more comprehensive discussion of this issue, although it seems a bit fastidious, But to do the procedure should hold this kind of thought, constantly strive for excellence ~ not: cool:.
First of all, the way the PHP array traversal for, while, foreach no longer say, do not know that you can go to the official PHP manual, but for, while, foreach traversal array of the writing also has a different, so the effect, Then see the program changed from the above link in the Lilov of the program, the specific content can be downloaded test source file renamed to Array_check.php can be run
Test one: PHP4.4.1, using a one-dimensional array
| Number |
Statement |
Time |
| 1 |
for ($i = 0; $i < $num = count ($arr); $i + +) |
0.1048162 (s) |
| 2 |
for ($i = 0, $num = count ($arr); $i < $num; $i + +) |
0.0698998 (s) |
| 3 |
while (list ($key, $val) = each ($arr)) |
0.1437800 (s) |
| 4 |
while (list (, $val) = each ($arr)) |
0.1226320 (s) |
| 5 |
while (list ($key,) = each ($arr)) |
0.1119628 (s) |
| 6 |
foreach ($arr as $key = $val) |
0.0972550 (s) |
| 7 |
foreach ($arr as $val) |
0.0649691 (s) |
You can see that the 7 is the fastest, but this way does not return the subscript, if the need to use subscript this method does not meet the requirements. Next is 2, see this writing and 1 What is the difference? 2 calculates the length of the array before the loop, and 1 calculates the length of the array for Each loop so that 2:1 is more efficient. However, 2 and 1 are the same, only the type of subscript is a number and the number of consecutive arrays. The third is 6, which is generally considered the quickest method, if you manipulate the subscript of the array and subscript there is a non-sequential number then there is no doubt that the choice of this method, as for the slow to say, forget these usage: roll:
Test two: PHP4.4.1, using a two-bit array
http://www.bkjia.com/PHPjc/446537.html www.bkjia.com true http://www.bkjia.com/PHPjc/446537.html techarticle PHP is still more commonly used, so I studied the PHP array traversal, here to take out and share with you, hope to be useful to everyone. About PHP array traversal, actually a lot of people have done ...