PHP中遍历关联数组的三种方法
方法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 —