2012-07-05 70 views
3

我有一個數組,其中包括以下數據:笨活動記錄從MySQL獲得記錄,其中IDS等於那些陣列

array(3) { [0]=> array(1) { ["project_id"]=> string(2) "14" } [1]=> array(1) { ["project_id"]=> string(2) "21" } [2]=> array(1) { ["project_id"]=> string(2) "13" } } 

我需要呼應從我的數據庫所在的項目ID是等於返回所有行那些在我的陣列中。

使用下面的代碼我怎麼才能得到記錄的ids等於那些在數據庫中?

function get_projects($id){ 
     $data = ''; 

     $this->db->where('id', $id); //HOW TO GET MULTIPLE IDS 
     $query = $this->db->get('projects'); 

     foreach ($query->result() as $row) { 
      $data[] = array(
       'id' => $row->id, 
       'user_id' => $row->user_id, 
       'project_name' => $row->project_name, 
      ); 
     } 

     return $data; 

    } 

回答

4

使用where_in();

$names = array('Frank', 'Todd', 'James'); 
$this->db->where_in('username', $names); 
// Produces: WHERE username IN ('Frank', 'Todd', 'James') 

更多見the user guide

+0

如何從多維數組中獲取數據並將其轉換爲標準數組,如您的答案? – masterharry 2012-07-05 14:39:26

+0

簡單的foreach循環應該可以很容易地從多維數組中獲取值並將其放入平面數組中。 – 2012-07-05 14:40:22

+0

你可以幫我做,或者我應該開一個新的問題,已經搜索,但無法找到答案,以滿足我的需求... – masterharry 2012-07-05 14:48:28