PHP 遍歷陣列的方法彙總

2021-09-06 21:49:08 字數 4608 閱讀 4475

foreach()是乙個用來遍歷陣列中資料的最簡單有效的方法。

#example1:

<?php

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

foreach ($colorsas$color)

?>

顯示結果:

do you like red? 

do you like blue? 

do you like green? 

do you like yellow?

while() 通常和 list(),each()配合使用。

#example2:

<?php

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

while(list($key,$val)= each($colors)) 

?>

顯示結果:

other list of red. 

other list of blue. 

other list of green. 

other list of yellow.

#example3:

<?php

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

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

?>

顯示結果:

the number is zero. 

the number is one. 

the number is two.

*****==== 以下是函式介紹 **********

mixed key(array input_array)

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

#example4

<?php

$capitals= array("ohio"=> "columbus","towa"=> "des moines","arizona"=> "phoenix");

echo "can you name the capitals of these states?

";while($key= key($capitals)) 

?>

顯示結果:

can you name the capitals of these states? 

ohio 

towa 

arizona

mixed reset(array input_array)

reset()函式用來將input_array的指標設定回陣列的開始位置。如果需要在乙個指令碼中多次檢視或處理同乙個陣列,就經常使用這個函式,另外這個函式還常用於排序結束時。

#example5 - 在#example1上追加**

<?php

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

foreach ($colorsas$color)

reset($colors);

while(list($key,$val)= each($colors)) 

?>

顯示結果:

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迴圈內,再次執行**,瀏覽器就會無休止地顯示結果。

array each(array input_array)

each()函式返回輸入陣列當前鍵/值對,並將指標推進乙個位置。返回的陣列包含四個鍵,鍵0和key包含鍵名,而鍵1和value包含相應的資料。如果執行each()前指標位於陣列末尾,則返回false。

#example6

<?php

$capitals= array("ohio"=> "columbus","towa"=> "des moines","arizona"=> "phoenix");

$s1= each($capitals);

print_r($s1);

?>

顯示結果:

array ( [1] => columbus [value] => columbus [0] => ohio [key] => ohio )

mixed current(array target_array)

current()函式返回位於target_array陣列當前指標位置的陣列值。與next()、prev()、和end()函式不同,current()不移動指標。 

next()函式返回緊接著放在當前陣列指標的下乙個位置的陣列值。 

prev()函式返回位於當前指標的前乙個位置的陣列值,如果指標本來就位於陣列的第乙個位置,則返回false。 

end()函式將指標移向target_array的最後乙個位置,並返回最後乙個元素。

#example7

<?php

echo $fruit."

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

echo $fruit."

";echo $fruit."

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

echo $fruit."

";?>

顯示結果:

**********= 下面來測試三種遍歷陣列的速度 **********=

一般情況下,遍歷乙個陣列有三種方法,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

<?php

$arr= array();

for($i= 0; $i< 50000; $i++)

function getruntime()

######################################

$time_start= getruntime();

for($i= 0; $i< count($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))

$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)

$time_end= getruntime();

$time_used= $time_end- $time_start;

echo 'used time of foreach:'.round($time_used, 7).'(s)

';?>

測試結果:

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還可以遍歷類的屬性。

PHP 遍歷陣列的方法彙總

from foreach 是乙個用來遍歷陣列中資料的最簡單有效的方法。example1 colors array red blue green yellow foreach colorsas color 顯示結果 do you like red?do you like blue?do you lik...

PHP 遍歷陣列的方法彙總

foreach 是乙個用來遍歷陣列中資料的最簡單有效的方法。example1 colors array red blue green yellow foreach colors as color 顯示結果 do you like red?do you like blue?do you like gr...

php遍歷陣列的方法分享

在php中陣列分為兩類www.cppcns.com 數字索引陣列和關聯陣列。其中數字索引陣列和c語言中的陣列一樣,下標是為0,1,2 而關聯陣列下標可能是任意型別,與其它語言中的hash,map等結構相似。方法1 foreach 複製 如下 sports array football good sw...