PHP 遍歷數組的方法彙總

1. foreach()

foreach()是一個用來遍歷數組中數據的最簡單有效的方法。php

#example1:api

  1. <?php數組

  2. $colors= array('red','blue','green','yellow');瀏覽器

  3. foreach ($colorsas$color){app

  4. echo "Do you like $color? <br />";函數

  5. }測試

  6. ?>ui


顯示結果:spa

Do you like red? 
Do you like blue? 
Do you like green? 
Do you like yellow?指針

2. while()

while() 一般和 list(),each()配合使用。

#example2:

  1. <?php

  2. $colors= array('red','blue','green','yellow');

  3. while(list($key,$val)= each($colors)) {

  4. echo "Other list of $val.<br />";

  5. }

  6. ?>

顯示結果:

Other list of red. 
Other list of blue. 
Other list of green. 
Other list of yellow.

3. for()

#example3:

  1. <?php

  2. $arr= array ("0"=> "zero","1"=> "one","2"=> "two");

  3. for ($i= 0;$i< count($arr); $i++){

  4. $str= $arr[$i];

  5. echo "the number is $str.<br />";

  6. }

  7. ?>

顯示結果:

the number is zero. 
the number is one. 
the number is two.

========= 如下是函數介紹 ==========

key()

mixed key(array input_array)

key()函數返回input_array中位於當前指針位置的鍵元素。

#example4

  1. <?php

  2. $capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");

  3. echo "<p>Can you name the capitals of these states?</p>";

  4. while($key= key($capitals)) {

  5. echo $key."<br />";

  6. next($capitals);

  7. //每一個key()調用不會推動指針。爲此要使用next()函數

  8. }

  9. ?>

顯示結果:

Can you name the capitals of these states? 
Ohio 
Towa 
Arizona

reset()

mixed reset(array input_array)

reset()函數用來將input_array的指針設置回數組的開始位置。若是須要在一個腳本中屢次查看或處理同一個數組,就常常使用這個函數,另外這個函數還經常使用於排序結束時。

#example5 - 在#example1上追加代碼

  1. <?php

  2. $colors= array('red','blue','green','yellow');

  3. foreach ($colorsas$color){

  4. echo "Do you like $color? <br />";

  5. }

  6. reset($colors);

  7. while(list($key,$val)= each($colors)) {

  8. echo "$key=> $val<br />";

  9. }

  10. ?>

顯示結果:

Do you like red? 
Do you like blue? 
Do you like green? 
Do you like yellow? 
0 => red 
1 => blue 
2 => green 
3 => yellow

注意:將一個數組賦值給另外一個數組時會重置原來的數組指針,所以在上例中若是咱們在循環內部將 $colors 賦給了另外一個變量的話將會致使無限循環。 
例如將 $s1 = $colors; 添加到while循環內,再次執行代碼,瀏覽器就會無休止地顯示結果。

each()

array each(array input_array)

each()函數返回輸入數組當前鍵/值對,並將指針推動一個位置。返回的數組包含四個鍵,鍵0和key包含鍵名,而鍵1和value包含相應的數據。若是執行each()前指針位於數組末尾,則返回FALSE。

#example6

  1. <?php

  2. $capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");

  3. $s1= each($capitals);

  4. print_r($s1);

  5. ?>

顯示結果:

Array ( [1] => Columbus [value] => Columbus [0] => Ohio [key] => Ohio )

current(),next(),prev(),end()

mixed current(array target_array)

current()函數返回位於target_array數組當前指針位置的數組值。與next()、prev()、和end()函數不一樣,current()不移動指針。 
next()函數返回緊接着放在當前數組指針的下一個位置的數組值。 
prev()函數返回位於當前指針的前一個位置的數組值,若是指針原本就位於數組的第一個位置,則返回FALSE。 
end()函數將指針移向target_array的最後一個位置,並返回最後一個元素。

#example7

  1. <?php

  2. $fruits= array("apple","orange","banana");

  3. $fruit= current($fruits); //return "apple"

  4. echo $fruit."<br />";

  5. $fruit= next($fruits); //return "orange"

  6. echo $fruit."<br />";

  7. $fruit= prev($fruits); //return "apple"

  8. echo $fruit."<br />";

  9. $fruit= end($fruits); //return "banana"

  10. echo $fruit."<br />";

  11. ?>

顯示結果:

apple 
orange 
apple 
banana

=========== 下面來測試三種遍歷數組的速度 ===========

通常狀況下,遍歷一個數組有三種方法,for、while、foreach。其中最簡單方便的是foreach。下面先讓咱們來測試一下共同遍歷一個有50000個下標的一維數組所耗的時間。

測試環境: 
Intel Core Due2 2GHz 
2GB 1067MHz DDR3 
Mac OS X 10.5.7 
Apache 2.0.59 
MySQL 5.0.41 
PHP 5.2.6

#example8

  1. <?php

  2. $arr= array();

  3. for($i= 0; $i< 50000; $i++){

  4. $arr[]= $i*rand(1000,9999);

  5. }

  6. function GetRunTime()

  7. {

  8. list($usec,$sec)=explode(" ",microtime());

  9. return ((float)$usec+(float)$sec);

  10. }

  11. ######################################

  12. $time_start= GetRunTime();

  13. for($i= 0; $i< count($arr); $i++){

  14. $str= $arr[$i];

  15. }

  16. $time_end= GetRunTime();

  17. $time_used= $time_end- $time_start;

  18. echo 'Used time of for:'.round($time_used, 7).'(s)<br /><br />';

  19. unset($str, $time_start, $time_end, $time_used);

  20. ######################################

  21. $time_start= GetRunTime();

  22. while(list($key, $val)= each($arr)){

  23. $str= $val;

  24. }

  25. $time_end= GetRunTime();

  26. $time_used= $time_end- $time_start;

  27. echo 'Used time of while:'.round($time_used, 7).'(s)<br /><br />';

  28. unset($str, $key, $val, $time_start, $time_end, $time_used);

  29. ######################################

  30. $time_start= GetRunTime();

  31. foreach($arr as$key=> $val){

  32. $str= $val;

  33. }

  34. $time_end= GetRunTime();

  35. $time_used= $time_end- $time_start;

  36. echo 'Used time of foreach:'.round($time_used, 7).'(s)<br /><br />';

  37. ?>

測試結果:

Used time of for:0.0228429(s)

Used time of while:0.0544658(s)

Used time of foreach:0.0085628(s)

通過反覆屢次測試,結果代表,對於遍歷一樣一個數組,foreach速度最快,最慢的則是while。從原理上來看,foreach是對數組副本進行操做(經過拷貝數組),而while則經過移動數組內部指標進行操做,通常邏輯下認爲,while應該比foreach快(由於foreach在開始執行的時候首先把數組複製進去,而while直接移動內部指標。),但結果剛剛相反。緣由應該是,foreach是PHP內部實現,而while是通用的循環結構。因此,在一般應用中foreach簡單,並且效率高。在PHP5下,foreach還能夠遍歷類的屬性。

相關文章
相關標籤/搜索