Foreach 函數(PHP4/PHP5) foreach 語法結構提供了遍歷數組的簡單方式。 foreach 僅能夠應用於數組和對象,如果嘗試應用於其他數據類型的變量,或者未初始化的變量將發出錯誤信息。 有兩種語法: [php] foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement 例子1: [php] <?php $arr=array(1,2,3,4); foreach($arr as $value){ echo $value."<br/>"; } ?> <?php $arr=array(1,2,3,4); foreach($arr as $value){ echo $value."<br/>"; } ?> 例子2: [php] <?php $arr=array(1,2,3,4); foreach($arr as $key=>$value){ echo $key."=====>".$value."<br/>"; } ?> <?php $arr=array(1,2,3,4); foreach($arr as $key=>$value){ echo $key."=====>".$value."<br/>"; } ?>