1

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

1
  • Please dont show us pictures of code!! If its text then post it as text into your question, formatted. We cannot copy/paste from a picture and search engines cannot index code-pitures Commented Mar 25, 2021 at 11:14

2 Answers 2

1

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:

Sign up to request clarification or add additional context in comments.

Comments

1

you can use php funcs like array_map for this specific case:

$newArr = array_map(function($val){
    return $val['id_question'];
},$results);

this should give you the required result

5 Comments

Ok I tried it so $val contains $query->result_array() right? I got an error Undefined variable: results, what should $results contain?
@MuhammadFaisal Wrong. $results is the array you're mapping. $val is every individual value from it. When you don't understand a function, read the manual.
Also, this is essentially a roundabout way to do array_column.
@El_Vanja I didn't use the answer above, but tried to use array_column() as you said and it worked perfectly, thanks!
@El_Vanja I didn't knew about this function. thank for the lesson.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.