下面介绍PHP中遍历关联数组的三种方法:
方法1:foreach
view source print?
4
|
'swimming' => 'very well',
|
5
|
'running' => 'not good');
|
6
|
foreach ($sports as $key => $value) {
|
7
|
echo $key.": ".$value."<br />";
|
输出结果:
football: goodswimming: very wellrunning: not good
方法2:each
view source print?
4
|
'swimming' => 'very well',
|
5
|
'running' => 'not good');
|
6
|
while ($elem = each($sports)) {
|
7
|
echo $elem['key'].": ".$elem['value']."<br />";
|
方法3:list & each
view source print?
4
|
'swimming' => 'very well',
|
5
|
'running' => 'not good');
|
6
|
while (list($key, $value) = each($sports)) {
|
7
|
echo $key.": ".$value."<br />";
|