我的关联数组:
$arr = array(
1 => "Value1",
2 => "Value2",
10 => "Value10"
);
Run Code Online (Sandbox Code Playgroud)
使用以下代码$v填充了$arr值
foreach($arr as $v){
echo($v); // Value1, Value2, Value10
}
Run Code Online (Sandbox Code Playgroud)
我如何获得$arr钥匙?
foreach(.....){
echo($k); // 1, 2, 10
}
Run Code Online (Sandbox Code Playgroud) 我正在运行这个DB调用来获取多维数组我试图获取每个键的键但是当我尝试它时出现空白或作为数组.
$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;
Run Code Online (Sandbox Code Playgroud)
这是我的php循环:
<?php foreach($places as $site): ?>
<h5><?=key($site)?></h5>
<?php foreach($site as $place): ?>
<h6><?=$place['place_name']?></h6>
<?php endforeach?>
<?php endforeach ?>
Run Code Online (Sandbox Code Playgroud)
此外,当我运行一个只吐出数组的测试时,这就是结果,我想渲染的是[费城]
[Philadelphia] => Array
(
[0] => Array
(
[place_name] => XYX …Run Code Online (Sandbox Code Playgroud)