foreach是php4才引入的,以下是教学方案,此方案易于被学习接受。
foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement
$show=array('1','2','3','4','7','9'); foreach ($show as $key) { echo $key; }
以下为手册中的例子。
<?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } // $arr is now array(2, 4, 6, 8) ?>第二种形式:
$show=array('1','2','3','4','7','9'); foreach ($show as $key=>$value) { echo $show[$key]; //echo $value; }输出为: 123479