以上是一些数组遍历的例子。
1,foreach遍历数组
<?php //foreach遍历 foreach (array_expression as $value){} foreach (array_expression as $key => $value){} <?PHP $speed = array(50,120,180,240,380); foreach($speed as $keys=>$values){ echo $keys."=>".$values."<br />"; } ?>
运行结果:
0=>50
1=>120
2=>180
3=>240
4=>380
2,while循环遍历数组
while循环遍历一般结合list函数。
<?PHP //while循环遍历 //by www.jb200.com $staff = array( array("姓名","性别","年龄"), array("小张","男",24), array("小王","女",25), array("小李","男",23) ); echo "<table border=2>"; while(list($keys,$value) = each($staff)){ list($name,$sex,$age) = $value; echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>"; } echo "</table>"; ?>
3,for循环遍历数组
<?PHP $speed = range(0,220,20); for($i =0;$i<count($speed);$i++) { echo $speed[$i]." "; } //by www.jb200.com ?>
运行结果:
0 20 40 60 80 100 120 140 160 180 200 220