PHP foreach循环键值 [英] PHP foreach loop key value

查看:181
本文介绍了PHP foreach循环键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行这个数据库调用让我多维数组
我想获得每个的键,但是当我尝试它出现空白或作为数组。

  $ root_array = array(); 
$ sites = $ this-> sites($ member_id);
foreach($ sites为$ 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($ place 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] =>数组

[place_name] => YYYY
[place_id] => ; 232323
[place_status] => 0


解决方案


$ b

  foreach($ array as $ key => $ value)


I am running this DB call to get me multi-dimensional array I am trying to get the keys of each but when I try it comes up blank or as array.

$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;

here is my php that loops through:

<?php foreach($places as $site): ?>
    <h5><?=key($site)?></h5>
        <?php foreach($site as $place): ?>
            <h6><?=$place['place_name']?></h6>
        <?php endforeach?>

<?php endforeach ?>

Also when I run a test which just spits out the array this is the result, What I am trying to render is [Philadelphia]

[Philadelphia] => Array
        (
            [0] => Array
                (
                    [place_name] => XYX
                    [place_id] => 103200
                    [place_status] => 0
                )

            [1] => Array
                (
                [place_name] => YYYY
                [place_id] => 232323
                [place_status] => 0
            )

解决方案

You can access your array keys like so:

foreach ($array as $key => $value)

这篇关于PHP foreach循环键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆