php——學習筆記,foreach循環

複習一下,使用each語句時,數組指針不會被重置 需要reset

而使用foreach的時候,數組指針會被自動重置

foreach不支持用“@”來禁止錯誤信息

foreach只能用於數組和對象

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>應用foreach語句遍歷數組</title>


<body>


<?php
$name = array("1"=>"iPhone5","2"=>"數碼相機","3"=>"聯想電腦","4"=>"天王表");
$price = array("1"=>"79999元","2"=>"3000元","3"=>"5600元","4"=>"3600元");
$counts = array("1"=>1,"2"=>1,"3"=>2,"4"=>1);
echo '
<table width="580" border="1" cellpadding="1" cellspacing="1">
          <tr>
            <td width="145" align="center"   >商品名稱</td>
            <td width="145" align="center"   >價 格</td>
            <td width="145" align="center"   >數量</td>
            <td width="145" align="center"   >金額</td>
  </tr>';
foreach($name as $key=>$value)
{ //以book數組做循環,輸出鍵和值
    echo 
'<tr>
            <td height="25" align="center" >'.$value.'</td>
            <td align="center"  >'.$price[$key].'</td>    
            <td align="center"  >'.$counts[$key].'</td>
            <td align="center"  >'.$counts[$key]*$price[$key].'</td>
</tr>';
}
echo
 '</table>';
?>


</body>
</html>

可以這樣用foreach輸出數組

可以得到數組元素名和元素值,也可以只得到元素值

foreach(數組名as元素名=>元素值)

發佈了44 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章