最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2014年07月05日 10:20:21   投稿:junjie  
這篇文章主要介紹了PHP數(shù)組遍歷知識匯總,本文包含3種遍歷方法、7個數(shù)組指針操作函數(shù)、對3種數(shù)組遍歷的速度測試等內(nèi)容,需要的朋友可以參考下

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

1. foreach()

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

#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中位于當前指針位置的鍵元素。

#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);
//每個key()調(diào)用不會推進指針。為此要使用next()函數(shù)
}
?>

顯示結(jié)果:

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

reset()

mixed reset(array input_array)

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

#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

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

each()

array each(array input_array)

each()函數(shù)返回輸入數(shù)組當前鍵/值對,并將指針推進一個位置。返回的數(shù)組包含四個鍵,鍵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ù)組當前指針位置的數(shù)組值。與next()、prev()、和end()函數(shù)不同,current()不移動指針。
next()函數(shù)返回緊接著放在當前數(shù)組指針的下一個位置的數(shù)組值。
prev()函數(shù)返回位于當前指針的前一個位置的數(shù)組值,如果指針本來就位于數(shù)組的第一個位置,則返回FALSE。
end()函數(shù)將指針移向target_array的最后一個位置,并返回最后一個元素。


#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

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

一般情況下,遍歷一個數(shù)組有三種方法,for、while、foreach。其中最簡單方便的是foreach。下面先讓我們來測試一下共同遍歷一個有50000個下標的一維數(shù)組所耗的時間。

測試環(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 />';
?>

測試結(jié)果:

Used time of for:0.0228429(s)

Used time of while:0.0544658(s)

Used time of foreach:0.0085628(s)

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

相關(guān)文章

  • ZendFramework框架實現(xiàn)連接兩個或多個數(shù)據(jù)庫的方法

    ZendFramework框架實現(xiàn)連接兩個或多個數(shù)據(jù)庫的方法

    這篇文章主要介紹了ZendFramework框架實現(xiàn)連接兩個或多個數(shù)據(jù)庫的方法,涉及ZendFramework框架配置文件與數(shù)據(jù)庫操作相關(guān)技巧,需要的朋友可以參考下
    2016-12-12
  • 深入理解PHP 數(shù)組之count 函數(shù)

    深入理解PHP 數(shù)組之count 函數(shù)

    下面小編就為大家?guī)硪黄钊肜斫釶HP 數(shù)組之count 函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • PHP獲取MAC地址的具體實例

    PHP獲取MAC地址的具體實例

    分享一例php取得機器mac地址的代碼,學(xué)習(xí)下php讀取硬件信息的方法,此代碼適用于windows、linux系統(tǒng)。有需要的朋友參考學(xué)習(xí)下
    2013-12-12
  • php實現(xiàn)當前頁面點擊下載文件的實例代碼

    php實現(xiàn)當前頁面點擊下載文件的實例代碼

    下面小編就為大家?guī)硪黄猵hp實現(xiàn)當前頁面點擊下載文件的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • thinkPHP分組后模板無法加載問題解決方法

    thinkPHP分組后模板無法加載問題解決方法

    這篇文章主要介紹了thinkPHP分組后模板無法加載問題解決方法,分析了thinkPHP分組后模板無法加載的原因與相應(yīng)的設(shè)置方法,需要的朋友可以參考下
    2016-07-07
  • zend framework文件上傳功能實例代碼

    zend framework文件上傳功能實例代碼

    zend framework文件上傳功能實例代碼,php的版本5.3.8,zend framework的版本1.12,看下面的代碼吧,有注釋
    2013-12-12
  • workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用的示例代碼

    workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用的示例代碼

    聊天功能是很常見的一種功能,Workerman是一款開源高性能異步PHP socket即時通訊框架。這篇文章主要介紹了workerman結(jié)合laravel開發(fā)在線聊天應(yīng)用,感興趣的小伙伴們可以參考一下
    2018-10-10
  • thinkphp中ajax與php響應(yīng)過程詳解

    thinkphp中ajax與php響應(yīng)過程詳解

    這篇文章主要介紹了thinkphp中ajax與php響應(yīng)過程,以實例形式詳細分析了ThinkPHP框架中Ajax響應(yīng)方式的處理原理及實現(xiàn)過程,非常具有實用價值,需要的朋友可以參考下
    2014-12-12
  • Laravel框架定時任務(wù)2種實現(xiàn)方式示例

    Laravel框架定時任務(wù)2種實現(xiàn)方式示例

    這篇文章主要介紹了Laravel框架定時任務(wù)2種實現(xiàn)方式,結(jié)合實例形式較為詳細的分析了Laravel框架定時任務(wù)相關(guān)實現(xiàn)方法及操作注意事項,需要的朋友可以參考下
    2018-12-12
  • laravel框架中間件簡單使用方法示例

    laravel框架中間件簡單使用方法示例

    這篇文章主要介紹了laravel框架中間件簡單使用方法,結(jié)合實例形式詳細分析了laravel框架中間件的功能、原理、用法及操作注意事項,需要的朋友可以參考下
    2020-01-01

最新評論

玛纳斯县| 潍坊市| 旬邑县| 桂林市| 平湖市| 汉寿县| 乳山市| 全南县| 永春县| 新巴尔虎左旗| 锡林浩特市| 灵宝市| 偃师市| 太仓市| 白沙| 丘北县| 来凤县| 克山县| 砚山县| 舞阳县| 棋牌| 格尔木市| 尼木县| 常州市| 金沙县| 阜阳市| 桃园县| 宜城市| 肃北| 安溪县| 丹棱县| 泰和县| 错那县| 汶上县| 广元市| 广西| 修水县| 阿拉尔市| 湘阴县| 商洛市| 龙井市|