PHP數(shù)組遍歷的方法匯總-創(chuàng)新互聯(lián)

本篇內(nèi)容主要講解“PHP數(shù)組遍歷的方法匯總”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“PHP數(shù)組遍歷的方法匯總”吧!

創(chuàng)新互聯(lián)服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過(guò)十余年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專(zhuān)業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對(duì)網(wǎng)站進(jìn)行做網(wǎng)站、成都做網(wǎng)站、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶網(wǎng)站對(duì)外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。

一、數(shù)組遍歷的3個(gè)方法介紹

1. foreach()

foreach()是一個(gè)用來(lái)遍歷數(shù)組中數(shù)據(jù)的最簡(jiǎn)單有效的方法。

#example1:


復(fù)制代碼 代碼如下:


<?php
$colors= array('red','blue','green','yellow');
foreach ($colorsas$color){
echo "Do you like $color? <br />";
}
?>



顯示結(jié)果:

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

2. while()

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

#example2:


復(fù)制代碼 代碼如下:


<?php
$colors= array('red','blue','green','yellow');
while(list($key,$val)= each($colors)) {
echo "Other list of $val.<br />";
}
?>


顯示結(jié)果:

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

3. for()

#example3:


復(fù)制代碼 代碼如下:


<?php
$arr= array ("0"=> "zero","1"=> "one","2"=> "two");
for ($i= 0;$i< count($arr); $i++){
$str= $arr[$i];
echo "the number is $str.<br />";
}
?>


顯示結(jié)果:

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

二、數(shù)組指針操作函數(shù)介紹

key()

mixed key(array input_array)

key()函數(shù)返回input_array中位于當(dāng)前指針位置的鍵元素。

#example4


復(fù)制代碼 代碼如下:


<?php
$capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
echo "<p>Can you name the capitals of these states?</p>";
while($key= key($capitals)) {
echo $key."<br />";
next($capitals);
//每個(gè)key()調(diào)用不會(huì)推進(jìn)指針。為此要使用next()函數(shù)
}
?>

顯示結(jié)果:

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

reset()

mixed reset(array input_array)

reset()函數(shù)用來(lái)將input_array的指針設(shè)置回?cái)?shù)組的開(kāi)始位置。如果需要在一個(gè)腳本中多次查看或處理同一個(gè)數(shù)組,就經(jīng)常使用這個(gè)函數(shù),另外這個(gè)函數(shù)還常用于排序結(jié)束時(shí)。

#example5 - 在#example1上追加代碼

復(fù)制代碼 代碼如下:


<?php
$colors= array('red','blue','green','yellow');
foreach ($colorsas$color){
echo "Do you like $color? <br />";
}
reset($colors);
while(list($key,$val)= each($colors)) {
echo "$key=> $val<br />";
}
?>


顯示結(jié)果:

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

注意:將一個(gè)數(shù)組賦值給另一個(gè)數(shù)組時(shí)會(huì)重置原來(lái)的數(shù)組指針,因此在上例中如果我們?cè)谘h(huán)內(nèi)部將 $colors 賦給了另一個(gè)變量的話將會(huì)導(dǎo)致無(wú)限循環(huán)。
例如將 $s1 = $colors; 添加到while循環(huán)內(nèi),再次執(zhí)行代碼,瀏覽器就會(huì)無(wú)休止地顯示結(jié)果。

each()

array each(array input_array)

each()函數(shù)返回輸入數(shù)組當(dāng)前鍵/值對(duì),并將指針推進(jìn)一個(gè)位置。返回的數(shù)組包含四個(gè)鍵,鍵0和key包含鍵名,而鍵1和value包含相應(yīng)的數(shù)據(jù)。如果執(zhí)行each()前指針位于數(shù)組末尾,則返回FALSE。

#example6


復(fù)制代碼 代碼如下:


<?php
$capitals= array("Ohio"=> "Columbus","Towa"=> "Des Moines","Arizona"=> "Phoenix");
$s1= each($capitals);
print_r($s1);
?>

顯示結(jié)果:

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


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

mixed current(array target_array)

current()函數(shù)返回位于target_array數(shù)組當(dāng)前指針位置的數(shù)組值。與next()、prev()、和end()函數(shù)不同,current()不移動(dòng)指針。
next()函數(shù)返回緊接著放在當(dāng)前數(shù)組指針的下一個(gè)位置的數(shù)組值。
prev()函數(shù)返回位于當(dāng)前指針的前一個(gè)位置的數(shù)組值,如果指針本來(lái)就位于數(shù)組的第一個(gè)位置,則返回FALSE。
end()函數(shù)將指針移向target_array的最后一個(gè)位置,并返回最后一個(gè)元素。


#example7


復(fù)制代碼 代碼如下:


<?php
$fruits= array("apple","orange","banana");
$fruit= current($fruits); //return "apple"
echo $fruit."<br />";
$fruit= next($fruits); //return "orange"
echo $fruit."<br />";
$fruit= prev($fruits); //return "apple"
echo $fruit."<br />";
$fruit= end($fruits); //return "banana"
echo $fruit."<br />";
?>


顯示結(jié)果:

apple
orange
apple
banana

三、測(cè)試三種遍歷數(shù)組的速度

一般情況下,遍歷一個(gè)數(shù)組有三種方法,for、while、foreach。其中最簡(jiǎn)單方便的是foreach。下面先讓我們來(lái)測(cè)試一下共同遍歷一個(gè)有50000個(gè)下標(biāo)的一維數(shù)組所耗的時(shí)間。

測(cè)試環(huán)境:
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


復(fù)制代碼 代碼如下:


<?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)<br /><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 /><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 /><br />';
?>


測(cè)試結(jié)果:

Used time of for:0.0228429(s)

Used time of while:0.0544658(s)

Used time of foreach:0.0085628(s)

經(jīng)過(guò)反復(fù)多次測(cè)試,結(jié)果表明,對(duì)于遍歷同樣一個(gè)數(shù)組,foreach速度最快,最慢的則是while。從原理上來(lái)看,foreach是對(duì)數(shù)組副本進(jìn)行操作(通過(guò)拷貝數(shù)組),而while則通過(guò)移動(dòng)數(shù)組內(nèi)部指標(biāo)進(jìn)行操作,一般邏輯下認(rèn)為,while應(yīng)該比f(wàn)oreach快(因?yàn)閒oreach在開(kāi)始執(zhí)行的時(shí)候首先把數(shù)組復(fù)制進(jìn)去,而while直接移動(dòng)內(nèi)部指標(biāo)。),但結(jié)果剛剛相反。原因應(yīng)該是,foreach是PHP內(nèi)部實(shí)現(xiàn),而while是通用的循環(huán)結(jié)構(gòu)。所以,在通常應(yīng)用中foreach簡(jiǎn)單,而且效率高。在PHP5下,foreach還可以遍歷類(lèi)的屬性。

到此,相信大家對(duì)“PHP數(shù)組遍歷的方法匯總”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

文章題目:PHP數(shù)組遍歷的方法匯總-創(chuàng)新互聯(lián)
文章位置:http://muchs.cn/article24/ippje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、云服務(wù)器Google、軟件開(kāi)發(fā)、響應(yīng)式網(wǎng)站、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作