方法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'
);
//each — 返回数组中当前的键/值对并将数组指针向前移动一步
while ($elem = each($sports)) {
echo $elem['key'].": ".$elem['value']."<br />";
}
?>
方法3:list & each
<?php
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not good'
);
//list — 把数组中的值赋给一些变量
while (list($key, $value) = each($sports)) {
echo $key.": ".$value."<br />";
}
?>
来源:oschina
链接:https://my.oschina.net/u/778827/blog/113207