Search code examples
sqlarrayscodeigniterconcatenation

Array CONCAT codeigniter 3


I'm working on a jobdesk management system but I have a problem that I can't solve. Can anyone help me?

I have Array concat :

array(2) {
  [0]=>
  array(10) {
    ["project_id"]=>
    string(1) "1"
    ["project_name"]=>
    string(11) "WebSite KMI"
    ["member_id"]=>
    string(7) "13,36,8"
  }

}

how to change the array above to be like this :

array(2) {
  [0]=>
  array(10) {
    ["project_id"]=>
    string(1) "1"
    ["project_name"]=>
    string(11) "WebSite KMI"
    ["member_id"]=>
                  [1] => string(1) "13"
                  [2] => string(2) "36"
                  [3] => string(2) "8"
    
  }
}

this is my script :

function my_data()
    {

        $this->db->select('project.*,GROUP_CONCAT(member_id) AS member_id');
        $this->db->from('project');
        $this->db->group_by("project.project_id");
        $this->db->join('project_meta', 'project.project_id = project_meta.project_id', 'left');
        $query = $this->db->get();

        return $query->result_array();
    }

Solution

  • Assuming your array is called $projects, you could use this:

    foreach ($projects as $key => $project) {
        $projects[$key]['member_id'] = explode(',', $project['member_id']);
    }
    

    For each project, split the member_ids on the , using explode and assign the result back onto the array.