本文介紹PHP中遍歷數組的foreach、list、each三個函數的使用方法:
方法1:foreach
<?php
$sports = array(
football => good,
swimming => very well,
running => not good);
foreach ($sports as $key => $value) {
echo $key.": ".$value."<br />";
?>
輸出結果:
football: good
swimming: very well
running: not good
方法2:each
<?php
$sports = array(
football => good,
swimming => very well,
running => not good);
while ($elem = each($sports)) {
echo $elem[key].": ".$elem[value]."<br />";
?>
方法3:list & each
<?php
$sports = array(
football => good,
swimming => very well,
running => not good);
while (list($key, $value) = each($sports)) {
echo $key.": ".$value."<br />";
?>