遍历关联数组

PHP中遍历关联数组的三种方法

六月ゝ 毕业季﹏ 提交于 2019-12-02 02:56:20
方法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 —