復制代碼 代碼如下:
<&#63;php
$colors= array('red','blue','green','yellow');
foreach ($colorsas$colo " /> 中国拍三a级的明星女,小女生RAPPER入口,东北女人一级毛片

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP數組遍歷知識匯總(包含遍歷方法、數組指針操作函數、數組遍歷測速)

一、數組遍歷的3個方法介紹

1. foreach()

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

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

顯示結果:

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.<br />";
}
?>
顯示結果:

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.<br />";
}
?>
顯示結果:

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 "<p>Can you name the capitals of these states?</p>";
while($key= key($capitals)) {
echo $key."<br />";
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? <br />";
}
reset($colors);
while(list($key,$val)= each($colors)) {
echo "$key=> $val<br />";
}
?>

顯示結果:

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."<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 />";
?>
顯示結果:

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)<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 />';
?>
測試結果:

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技術PHP數組遍歷知識匯總(包含遍歷方法、數組指針操作函數、數組遍歷測速),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 欧美18精品久久久无码午夜福利 | 亚洲欧美自拍明星换脸 | 床伴在线观看免费高清完整泰剧第四集 | 国产亚洲欧美在线观看三区 | 97超碰97资源在线观看视频 | 青柠在线观看视频在线高清 | 久久精品国产亚洲AV天美18 | 久久久综合中文字幕久久 | 在线播放av欧美无码碰 | 一个人免费视频在线观看高清版 | 老师的玉足高跟鞋满足我 | 无限资源日本2019版 | 白嫩美女直冒白浆 | 色99久久久久高潮综合影院 | 欧美性黑吊xxx | 日日噜噜大屁股熟妇 | 成人无码精品一区二区在线观看 | 精品久久日日躁夜夜躁AV | 国产人妻精品午夜福利免费不卡 | 色欲天天婬色婬香影院 | 国语自产一区视频 | 97精品视频在线观看 | 国产在线成人一区二区三区 | 人妻中文字幕乱人伦在线 | 夜色55夜色66亚洲精品网站 | jzz大全18 | AV无码久久无遮挡国产麻豆 | 欧美黑人巨大性极品hd欧 | 成人在线高清不卡免费视频 | 美女国产毛片A区内射 | 综合激情区视频一区视频二区 | 麻豆AV久久无码精品九九 | yellow在线中文 | 免费无遮挡又黄又爽网站 | 天天国产在线精品亚洲 | 日本激情网址 | 99精品国产福利在线观看 | 超碰97人人做人人爱少妇 | JIZZ幻女大全| 黄页免费观看 | 午夜办公室在线观看高清电影 |