PHP數組遍曆知識匯總(包含遍曆方法、數組指標操作函數、數組遍曆測速)_PHP教程

來源:互聯網
上載者:User
一、數組遍曆的3個方法介紹

1. foreach()

foreach()是一個用來遍曆數組中資料的最簡單有效方法。

#example1:
複製代碼 代碼如下:
<?php
$colors= array('red','blue','green','yellow');
foreach ($colorsas$color){
echo "Do you like $color?
";
}
?>

顯示結果:

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

2. while()

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

#example2:
複製代碼 代碼如下:
<?php
$colors= array('red','blue','green','yellow');
while(list($key,$val)= each($colors)) {
echo "Other list of $val.
";
}
?>
顯示結果:

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

3. for()

#example3:
複製代碼 代碼如下:
<?php
$arr= array ("0"=> "zero","1"=> "one","2"=> "two");
for ($i= 0;$i< count($arr); $i++){
$str= $arr[$i];
echo "the number is $str.
";
}
?>
顯示結果:

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

二、數組指標操作函數介紹

key()

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)) {
echo $key."
";
next($capitals);
//每個key()調用不會推進指標。為此要使用next()函數
}
?>

顯示結果:

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

reset()

mixed reset(array input_array)

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

#example5 - 在#example1上追加代碼

複製代碼 代碼如下:
<?php
$colors= array('red','blue','green','yellow');
foreach ($colorsas$color){
echo "Do you like $color?
";
}
reset($colors);
while(list($key,$val)= each($colors)) {
echo "$key=> $val
";
}
?>

顯示結果:

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
複製代碼 代碼如下:
<?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 )


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

mixed current(array target_array)

current()函數返回位於target_array數組當前指標位置的數組值。與next()、prev()、和end()函數不同,current()不移動指標。
next()函數返回緊接著放在當前數組指標的下一個位置的數組值。
prev()函數返回位於當前指標的前一個位置的數組值,如果指標本來就位於數組的第一個位置,則返回FALSE。
end()函數將指標移向target_array的最後一個位置,並返回最後一個元素。


#example7
複製代碼 代碼如下:
<?php
$fruits= array("apple","orange","banana");
$fruit= current($fruits); //return "apple"
echo $fruit."
";
$fruit= next($fruits); //return "orange"
echo $fruit."
";
$fruit= prev($fruits); //return "apple"
echo $fruit."
";
$fruit= end($fruits); //return "banana"
echo $fruit."
";
?>
顯示結果:

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
複製代碼 代碼如下:
<?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)

';
?>
測試結果:

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

http://www.bkjia.com/PHPjc/824613.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/824613.htmlTechArticle一、數組遍曆的3個方法介紹 1. foreach() foreach()是一個用來遍曆數組中資料的最簡單有效方法。 #example1: 複製代碼 代碼如下: php $colors= ar...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.