PHP遍历关联数组的方法介绍
PHP遍历关联数组的方法介绍
发布者:chinaitlab
日期:
2014-04-10 05:59:54 浏览次数:0 (共有_条评论)
查看评论 | 我要评论
在PHP中数组分为两类: 数字索引数组和关联数组。其中数字索引数组和C语言中的数组一样,下标是为0,1,2…而关联数组下标可能是任意类型,与其它语言中的hash,map等结构相似。
下面介绍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
>>更多交流,请到ChinaUnix【Linux系统管理论坛】:http://http://www.zjjv.com///bbs/forum-2-1.html
关键词:
相关文章
网友评论
已有0位网友发表了看法