2015-04-01 48 views
0

我想在使用CodeIgneter的視圖中使用PHP函數in_array如何使用函數in_array與CodeIgneter

這是方法

class User_details extends CI_Model { 

public function checkID(){ 
$query = $this->db->query("SELECT id FROM users"); 

return $query->result_array(); 

}

這是控制器

$this->load->model('user_details'); 

$data['allUserID'] = $this->user_details->checkID(); 
$this->load->view('user_details/content',$data); 

這是我想在視圖中

$id = $this->uri->segment(4);//user's id for example: 218 


if(in_array($id,$allUserID)){ 
echo 'exists'; 
} else { 
echo 'does not exist'; 
} 

使用的腳本不幸的是, e腳本不能按預期工作。

如果我檢查使用的print_r($ allUserID)我得到以下結果數組:

Array ([0] => Array ([id] => 217) [1] => Array ([id] => 218 ... 
...and so on... 

上週,我開始學習CodeIgneter,我不明白它的方法呢。

+1

沒有任何與'codeigneter'和'in_array'相關的函數用於在'array'中查找某些內容。你以錯誤的方式使用它。它用於單維陣列 – 2015-04-01 13:47:15

回答

1

in_array()接受單維數組,並傳遞二維數組。嘗試將其轉換爲單維數組,如

$array = array_map('current', $allUserID); 

if(in_array($id, $array)){ 
echo 'exists'; 
} else { 
    echo 'does not exist'; 
} 
+0

感謝大家。是的,當然:我以錯誤的方式使用in_array()。 – user4265789 2015-04-01 13:57:24