我正在运行这个数据库调用让我多维数组 我想获得每个键,但当我尝试它出现空白或作为数组。PHP foreach循环键值
$root_array = array();
$sites = $this->sites($member_id);
foreach ($sites as $site){
$records = $this->db->select('p.name as place_name, p.id as place_id,p.active as place_status')
->from('places p')
->join('members_permissions pm','pm.sites_id = p.sites_id and pm.members_id ='.$member_id)
->where('p.active', 0)
->get();
$places = $records->result_array();
$places['name'] = $site['name'];
foreach($places as $place){
$root_array[$site['name']][] = $place;
}
}
return $root_array;
这里是我的PHP是遍历:
<?php foreach($places as $site): ?>
<h5><?=key($site)?></h5>
<?php foreach($site as $place): ?>
<h6><?=$place['place_name']?></h6>
<?php endforeach?>
<?php endforeach ?>
而且当我运行一个测试刚刚吐出来的是数组,这是结果, 我试图渲染为[费城]
[Philadelphia] => Array
(
[0] => Array
(
[place_name] => XYX
[place_id] => 103200
[place_status] => 0
)
[1] => Array
(
[place_name] => YYYY
[place_id] => 232323
[place_status] => 0
)
这个方法只返回'array' – matthewb 2009-12-02 18:12:07
什么,如果你echo $ key?罗。如果你在“费城”是其中一个关键的阵列上运行它,那么不是。你能显示一些代码吗? – 2009-12-02 18:17:49
这工作,我的mysql语句也有一个小问题,但我得到它与您的解决方案,谢谢你。 – matthewb 2009-12-02 18:38:17