Bhumi Singhal
Bhumi Singhal

Reputation: 8297

result_array() data in where clause in OR condition : Active records Codeigniter

result_array() for a query gives the following :

Array ( [0] => Array ( [id] => 1 ) [1] => Array ( [id] => 4 ) ) 

I want to use id=1 and id=4 in the where clause in the OR condition like the following :

$this->db->where_in('id',$query->result_array());

But the above causes error. Is there any direct way of doing the same?

Upvotes: 1

Views: 739

Answers (4)

Mike Miller
Mike Miller

Reputation: 3129

If you change your original query to use GROUP_CONCAT. Not an option in Active Record so would need to write your own SQL. something like

$query= $this->db->query('SELECT GROUP_CONCACT(id) AS ids FROM table WHERE condition=true');

Then you should be able to do

$this->db->where_in('id',$query->result_array()[0]['ids']);

Just noticed your on 5.3 so unfortunately that means this won't work you will have to do.

$result = $query->result_array();

Then pass in $result[0]['ids']

Upvotes: 0

Bhumi Singhal
Bhumi Singhal

Reputation: 8297

$result = $roleIdQuery->result_array();
$column = array_map(function($sub)
                           {
                               return $sub['id'];
                           }, $result);

I used this. Sadly.

Upvotes: 1

Karan Thakkar
Karan Thakkar

Reputation: 1467

You can simply use array_column

array_column($query->result_array(), 'id')

here is reference to it.

Upvotes: 1

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

because it has arrays within that array.

  $ids=array();
  $resultArray=$query->result_array();
  foreach($resultArray as $result){
   $ids[]=$result['id'];
  }

$ids // this is what you need to pass now..

Upvotes: 0

Related Questions