逐个读取数组值where子句

Here's my controller

$data['lvl0'] = $this->web_model->menulvl0();
if ($data['lvl0'] != ''){
  foreach($data['lvl0'] as $l0){
     $data['id'] = $l0['id'];
 $data['name'] = $l0['name'];
 $temp = explode(" ",$l0['id']);
 $data['lvl1'] = $this->web_model->menulvl1($temp);
 }
}

when i print_r($temp). the result is :

Array ( [0] => 10 ) 
Array ( [0] => 20 ) 
Array ( [0] => 30 ) 
Array ( [0] => 40 )

I want to put that array into query. here's my query :

SELECT id, name FROM A where level = 1 and parent = '$array'

I want that array are read one by one. So, query will be:

SELECT id, name FROM A where level = 1 and parent = '10'

and then

SELECT id, name FROM A where level = 1 and parent = '20'

and so on.

How can do that?

Use a foreach statement to sort through your array:

foreach($array as $val)
{
    $query="SELECT id, name FROM A where level = 1 and parent = '".$val."'";
}

You can either run it in there, or just make the queries.

Alternately you can use an implode and an in statement:

$vals=implode(",",$array);
$query="SELECT id, name FROM A where level = 1 and parent in(".$vals.")";

You can do so by using CI's active record

$query=$this->db->select('id, name')
->from('A')
->where('level',1)
->where_in('parent',$array)
->order_by('parent')
->get();

$result=$query->result();