Failed to create multidimensional array in codeigniter

I have the following code

public function get_request($requestid)
{
    $this->db->select('*');
    $this->db->from('instanthire as s');
    $this->db->join('instanthire_skills as ss', 's.id = ss.requestid');
    $this->db->where('s.id',$requestid);
    $query = $this->db->get();
    return $query->result_array();
}

The resulting array that I get is

Array
(
    [0] => Array
        (
            [id] => 1
            [userid] => 1
            [skills] => html
        )

    [1] => Array
        (
            [id] => 2
            [userid] => 1
            [skills] => core php
        )
)
Array
(
    [0] => Array
        (
            [id] => 3
            [userid] => 2
            [skills] => Core Java
        )

    [1] => Array
        (
            [id] => 4
            [userid] => 2
            [skills] => Advance Java
        )
    [2] => Array
        (
            [id] => 4
            [userid] => 2
            [skills] => .net
        )
)

The problem is that for each userid the number of arrays increases with increasing skills, I want to create one array for each user and the skills for this user must be in the main array. can anybody tell how i can fix my array

Table structure

instanthire

id  userid 
1     1
2     2

instanthire_skills

id  requestid  skills
1    1         html
2    1         core php
3    2         core java
4    2         advance
5    2         .net

desired output

Array
(
    [0] => Array
        (
            [id] => 1
            [userid] => 1
            [skills] => Array
                (
                    [0] => html
                    [1] => core php
                )
        )

    [1] => Array
        (
            [id] => 3
            [userid] => 2
            [skills] => Array
                (
                    [0] => core java
                    [1] => advance java
                    [2] => .net
                )
       )
)
+4
source share
1 answer

, , . , db, .

, mysql Group_Concat() () . explode() .

, :)

where ( userid instanthire - ?) $query->row_array() . , $query->result_array().

public function get_request($requestid)
{
    $this->db->select('instanthire.id, Group_Concat(instanthire_skills.skills) as skills');
    $this->db->from('instanthire');
    $this->db->join('instanthire_skills', 'instanthire.id = instanthire_skills.requested_id' , 'inner');
    $this->db->where('instanthire.userid', $requestid);
    $query = $this->db->get();
    $result = $query->row_array();
    $result['skills'] = explode(",", $result['skills']);
    return $result;
}

,

$data = $this->get_request(2);
var_dump($data);

get_request() $result = $query->row_array(); var_dump($result);,

array (size=2)
   'id' => string '2' (length=1)
   'skills' => string 'core java,advance,.net' (length=22)

get_request() var_dump($data);

array (size=2)
  'id' => string '2' (length=1)
  'skills' => 
    array (size=3)
      0 => string 'core java' (length=9)
      1 => string 'advance' (length=7)
      2 => string '.net' (length=4)

userid, . , .

+1

Source: https://habr.com/ru/post/1652429/


All Articles