我有一个阵列
district
,其中包含36个地区,我在地区ID上找到了他们的总裁兼秘书。
$districts = $this->dashboard->get_districts();
foreach ($districts AS $district)
{
$contacts = $this->dashboard->get_contacts($district["ID"]);
$result = array_merge($result, $contacts);
}
视图的加载是:
$finalArray["result"] = $result;
$this->load->view("admin/view_contacts.php", $finalArray);
$testarray = array(
"Attock"=>array(
"president"=>"gulzar",
"secretary"=>"musa"
),
"Bahawalnagar"=>array(
"president"=>"muzamil",
"secretary"=>"tania"
)
);
您需要在
$results
foreach($districts AS $district)
{
$result[$district['name']] = $this->dashboard->get_contacts($district["ID"]);
// ^^^^ this is of course a guess and depends on your column name
}
也是,假设您的
get_contacts()
方法是一个数据库查询,则进行
JOIN
并在一个数据库查询中获得必要的结果可能更有效。您仍然可以在结果上循环以构建所需的输出数组。
$districts=$this->dashboard->get_districts();
$returnArray = array();
foreach($districts AS $district)
{
$contacts=$this->dashboard->get_contacts($district["ID"]);
$returnArray[$district['name']]['president'] = //Insert president value here.
$returnArray[$district['name']]['secretary'] = //Insert secretary value here.
}