本文介紹了PHP foreach 循環(huán)鍵值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在運行這個數(shù)據(jù)庫調(diào)用來獲取多維數(shù)組我正在嘗試獲取每個鍵的鍵,但是當我嘗試時它出現(xiàn)空白或數(shù)組.
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;
這是我的 php 循環(huán):
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 ?>
此外,當我運行一個只是吐出數(shù)組的測試時,這就是結(jié)果,我試圖呈現(xiàn)的是[費城]
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
)
推薦答案
你可以像這樣訪問你的數(shù)組鍵:
You can access your array keys like so:
foreach ($array as $key => $value)
這篇關(guān)于PHP foreach 循環(huán)鍵值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!