如何从数组变量获取查询中的数组值


How to get array values in a query from an array variable

我有一个这样的模型

function show_city_n_area_from_id($data){ // $data is an array of ids
    $this->db->select('column1');
    $this->db->from('table');
    $this->db->where('id', $data);
    $query = $this->db->get();
    $query_result = $query_array();
    return $query_result;
}

我正在尝试从"column1"中提取所有值,以"$data"的数组形式从"中提取所有值,这是一个由多个 id 组成的数组。

提前谢谢。

我收到错误:

"where 子句"中的未知列"数组"从table中选择column1,其中 id = Array

根据您的

评论$data是一个 id 数组,因此您需要使用如下where_in

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

修改后的代码

function show_city_n_area_from_id($data){ 
   $this->db->select('column1'); 
   $this->db->from('table'); 
   $this->db->where_in('id', $data); 
   $query = $this->db->get(); 
   $query_result = $query_array(); 
   return $query_result; 
}

您还可以浏览 CI 用户指南

更新 1:

根据您的注释,您在对象中具有id,因此您可以将其转换为数组,如下所示:

$dbIDS = array();
foreach($data['ids'] as $value){
    $dbIDS[] = $value->vbc_item_id;
}

现在,您可以在where_in中使用$dbIDS