一尘不染

PHP foreach循环键值

php

我正在运行此数据库调用以获取多维数组,我试图获取每个数组的键,但是当我尝试将其显示为空白或数组时。

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

阅读 348

收藏
2020-05-26

共1个答案

一尘不染

您可以这样访问数组键:

foreach ($array as $key => $value)
2020-05-26