2012-01-12 47 views
2

以下工作(陣列由另一個MySQL查詢填寫):MySQL的

$array=(1,4,5,6,8); 
$query=SELECT * FROM table WHERE Id IN(".implode(",",$array)." 

但我怎麼可以做以下?:

$array=(
array('Id'=>1, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']), 
array('Id'=>4, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']), 
array('Id'=>5, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']), 
array('Id'=>6, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']), 
array('Id'=>8, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']), 
); 
$query=SELECT * FROM table WHERE Id IN(".implode(",",$array[]['Id'])." 

,所以我需要得到所有的ID,我怎麼能在這個多維數組中做到這一點?

謝謝!

回答

6

我只想做這樣的事情:

$id_list = array(); 

foreach($array as $item) { 
$id_list[] = $item['Id']; 
} 

$query = "SELECT * FROM table WHERE Id IN(".implode(',',$id_list).")"; 
1

另一種選擇:

$list = array_reduce($id_list,function($total,$cur_value){ 
    if(!is_null($total)) $total .= ','; 
    return $total.$cur_value['id']; 
    }); 
$query = "SELECT * FROM table WHERE Id IN(".$list.")";