我的模型中有一个功能:
public function get_job($lsnumber = FALSE) {
$this->db->join('administrator', 'job.idadministrator = administrator.idadministrator');
$this->db->join('artwork', 'job.idjob = artwork.idjob');
if($lsnumber === FALSE) {
$query = $this->db->get('job');
return $query->result_array();
}
}
返回这个让我在视图中使用foreach循环为数据库中的每个条目生成一行。
有时这些行会有共同点,例如idjob
规则中引用的join
字段。
目前,我正在获得一个输出(来自print_r
):
Array
(
[idjob] => 1
[lsnumber] => 12345
[custname] => Scott Brown (Customer)
[custemail] => sbrown@example.com
[custcompany] => Customer
[idadministrator] => 1
[complete] => 0
[administratorname] => Scott Brown (Administrator)
[administratoremail] => scott@example.com
[administratorjob] => Job
[administratorphone] => 01234 567890
[idartwork] => 1
[filename] => gb-usb1.jpg
[productname] => Bespoke USB Drive
[revision] => 0
[status] => N
)
Array
(
[idjob] => 1
[lsnumber] => 12345
[custname] => Scott Brown (Customer)
[custemail] => sbrown@example.com
[custcompany] => LSi (Customer)
[idadministrator] => 1
[complete] => 0
[administratorname] => Scott Brown (Administrator)
[administratoremail] => scott@example.com
[administratorjob] => Job
[administratorphone] => 01234 567890
[idartwork] => 2
[filename] => pa17.jpg
[productname] => Notebooks
[revision] => 0
[status] => Y
)
但是,我希望将数组分组并进行多维处理,如下所示:
Array
(
[idjob] => 1
[lsnumber] => 12345
[custname] => Scott Brown (Customer)
[custemail] => sbrown@example.com
[custcompany] => Customer
[idadministrator] => 1
[complete] => 0
[administratorname] => Scott Brown (Administrator)
[administratoremail] => scott@example.com
[administratorjob] => Job
[administratorphone] => 01234 567890
[artwork][0] => Array
(
[idartwork] => 1
[filename] => gb-usb1.jpg
[productname] => Bespoke USB Drive
[revision] => 0
[status] => N
)
[artwork][1] => Array
(
[idartwork] => 2
[filename] => pa17.jpg
[productname] => Notebooks
[revision] => 0
[status] => Y
)
)
我希望能够按idjob
对其进行分组,并构建一个加入它的所有artwork
的子数组。
我尝试了各种各样的事情:array_chunk()
,分组,各种各样。
答案 0 :(得分:0)
试试这个,你可能需要稍微改变一下:
function get_job(){
$data = array();
$data = $this->db->get('job')->result_array();
foreach( $data as $key=>$each ){
$data[$key] = $this->db->where('idadministrator', $each['idadministrator'])->get('administrator')->row_array();
$data[$key]['artwork'] = $this->db->where('idjob', $each['idjob'])->get('artwork')->result_array();
}
return $data;
}