php入門教程-foreach 數組的常用遍曆方法

來源:互聯網
上載者:User

第六章(2)數組的常用遍曆方法
count() 在PHP中在此函數中輸入一數組的頭部名,此數組中有多少個資料組成的 count就會輸出數字幾. sizeof 同作用.
foreach 迴圈結構.
例題:
$arr = array("hello"=>array(1,2,3,"bbb"=>4,5,6,7,"aaa"=>8),array("one","two"),100=>array("a",1,"b",2));
方法:
foreach($arr as $key=>$value)
{
 foreach($value as $k=>$v)
 {
  echo "$arr[".$key."][".$k."]=".$v."";
 }
 echo "<br>";
}
輸出:
$arr[hello][0]=1    $arr[hello][1]=2    $arr[hello][2]=3    $arr[hello][bbb]=4    $arr[hello][3]=5    $arr[hello][4]=6    $arr[hello][5]=7    $arr[hello][aaa]=8    $arr[0][0]=one    $arr[0][1]=two    $arr[100][0]=a    $arr[100][1]=1    $arr[100][2]=b    $arr[100][3]=2
方法:
while(list($key,$value) = each($arr))
{
 while(list($k,$v) = each($value))
 {
  echo $key."==>".$k."===>".$v."<br>";
 }
}
輸出:
hello==>0===>1    hello==>1===>2    hello==>2===>3    hello==>bbb===>4    hello==>3===>5    hello==>4===>6   hello==>5===>7
hello==>aaa===>8    0==>0===>one    0==>1===>two    100==>0===>a    100==>1===>1    100==>2===>b    100==>3===>2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.