0

我的模型中有一个函数:

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

我希望能够按 s 对它进行分组,并构建一个包含所有加入它idjob的 s 的子数组。artwork

我已经尝试过各种事情:array_chunk(),分组,各种。

4

1 回答 1

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;
}
于 2013-09-11T15:43:30.397 回答