Search code examples
phpmysqlarrayscodeigniter

Returns the query result as a pure array (index array) instead of an associative array PHP Codeigniter 3


So I want to use $this->db->where_not_in() using an array in its second parameters.

I know it works fine if I have an index array and use it like this:

$names = array('Frank', 'Todd', 'James');
$this->db->where_not_in('column', $names);

But the output array of my query looks like this, so I can't use this to the 2nd parameter where_not_in ():

Array (
   [0] => Array (
      [id_question] => PN21022601
   )
   [1] => Array (
      [id_question] => PN21022602
   )
)

I tried to convert it to an index array using array_values​​() but it just returned the same result as above.

I've also read the documentation from Codeigniter 3 here by trying the $query->result_array() method, as well as the $query->result() method and the results are the same.

My expectation is that I can produce an array like this:

Array (
   [0] => PN21022601
   [1] => PN21022602
)

Did I miss something?
Is this an error from the mysql query I made? Especially at group by syntax?
Here is my query to generate data like the one above:

$query = $this->db->query("
        SELECT e.id_pertanyaan
        
        FROM t_penilaian AS a
        JOIN t_penilaian_detail AS b ON a.id_penilaian = b.id_penilaian
        JOIN t_penilaian_detail_score AS c ON b.id_penilaian_detail = c.id_penilaian_detail
        JOIN m_pertanyaan_detail AS d ON c.id_pertanyaan_detail = d.id_pertanyaan_detail
        JOIN m_pertanyaan AS e ON d.id_pertanyaan = e.id_pertanyaan
        
        WHERE a.id_user = '$id_user'
        GROUP BY e.id_pertanyaan");

This is an example script for creating the database. and DB Designer to quickly see the relationship

Thanks for any help..


Solution

  • Se your query to return array like so:

    $results = $this->db->result_array();
    

    Having the results as array instead of object you can get just one of the columns as a normal array with array column.

    $results = array_column($this->db->result_array(), 'id_question');
    

    This way the results will be exactly like you want.

    More info about array column here: