文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>PHP 哈希表,关联数组,遍历方法大全

PHP 哈希表,关联数组,遍历方法大全

时间:2010-11-20  来源:春哥也编程

下面介绍PHP中遍历关联数组的三种方法:
方法1:foreach

view source print?
1 <?php
2 $sports = array(
3     'football' => 'good',
4     'swimming' => 'very well',
5     'running'  => 'not good');
6 foreach ($sports as $key => $value) {
7     echo $key.": ".$value."<br />";
8 ?>

输出结果:

football: goodswimming: very wellrunning: not good

方法2:each

view source print?
1 <?php
2 $sports = array(
3     'football' => 'good',
4     'swimming' => 'very well',
5     'running'  => 'not good');
6 while ($elem = each($sports)) {
7     echo $elem['key'].": ".$elem['value']."<br />";
8 ?>

方法3:list & each

view source print?
1 <?php
2 $sports = array(
3     'football' => 'good',
4     'swimming' => 'very well',
5     'running'  => 'not good');
6 while (list($key, $value) = each($sports)) {
7     echo $key.": ".$value."<br />";
8 ?>

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载