PHP遍历关联数组的方法介绍
2014-04-07 23:30
666 查看
在PHP中数组分为两类: 数字索引数组和关联数组。其中数字索引数组和C语言中的数组一样,下标是为0,1,2…而关联数组下标可能是任意类型,与其它语言中的hash,map等结构相似。
下面介绍PHP中遍历关联数组的三种方法:
程序运行结果:
程序运行结果:程序运行结果:
程序运行结果:
下面介绍PHP中遍历关联数组的三种方法:
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
each
<?php $sports = array( 'football' => 'good', 'swimming' => 'very well', 'running' => 'not good' ); while ($elem = each($sports)) { echo $elem['key'].": ".$elem['value']."<br />"; } ?>
程序运行结果:程序运行结果:
football: good swimming: very well running: not good
list & each
<?php $sports = array( 'football' => 'good', 'swimming' => 'very well', 'running' => 'not good' ); while (list($key, $value) = each($sports)) { echo $key.": ".$value."<br />"; } ?>
程序运行结果:
football: good swimming: very well running: not good
相关文章推荐
- PHP遍历关联数组的方法介绍
- PHP:遍历关联数组的方法介绍
- PHP中遍历关联数组的方法
- PHP遍历关联数组的几种方法
- PHP 哈希表,关联数组,遍历方法大全
- PHP中遍历关联数组的方法
- PHP中遍历关联数组的方法
- [php] PHP中遍历关联数组的三种方法
- PHP中遍历关联数组的方法
- php遍历关联数组的三种方法
- PHP中遍历关联数组的方法
- PHP搜索数组的实现方法介绍
- PHP 遍历数组的方法汇总
- php遍历数组的三种方法
- PHP 数组遍历方法大全(foreach,list,each)
- php通过asort()给关联数组按照值排序的方法
- PHP删除数组中空值的方法介绍
- PHP中数组合并的两种方法及区别介绍
- PHP关联数组的具体使用方法
- PHP中数组合并的两种方法及区别介绍