Codeigniter分组并创建多维数组 [英] Codeigniter group by and create multidimensional array

查看:141
本文介绍了Codeigniter分组并创建多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型中有一个函数:

I've got a function in my model:

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循环。

Returning this lets me generate a row for each entry in the database using a foreach loop in my view.

有时这些行会有共同点,例如 idjob

Sometimes these rows will have things in common, for example the idjob field as referenced in the join rule.

现在,我得到一个输出(从 join code> print_r )像这样:

At the moment, I'm getting an output (from print_r) like this:

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
)

但是,我想将数组分组和多维,类似这样:

However, I'd like the array to be grouped and multidimensional, something like this:

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 的子数组

I want to be able to group it by the idjob and build a sub-array of all the artworks joined to it.

我尝试过各种各样的事情: array_chunk(),group by,all sorts

I've tried all kinds of thing: array_chunk(), group by, all sorts.

推荐答案

尝试此操作,您可能需要稍微更改:

Try this, you may have to change it slightly:

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;
}

这篇关于Codeigniter分组并创建多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆