php ActiveRecord where_in() 与数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17180453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActiveRecord where_in() with array
提问by Twhyler
I have tried a few different approaches to this problem with no solutions so far. I am receiving this error message:
我已经尝试了几种不同的方法来解决这个问题,到目前为止还没有解决方案。我收到此错误消息:
Unknown column 'Array' in 'where clause'
“where 子句”中的未知列“Array”
SELECT * FROM (Articles
) WHERE id
IN (Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array, Array)
SELECT * FROM ( Articles
) WHERE id
IN (数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、数组、阵列,阵列,阵列,阵列,阵列,阵列,阵列,阵列,阵列,阵列,阵列,阵列)
Here is my model code:
这是我的模型代码:
function getStarNews($user_id) {
$this->db->select('id');
$this->db->where('user_id', $user_id);
$query = $this->db->get('table1');
foreach ($query->result_array() as $id)
{
//echo $id['id']."<br>"; //displays all of the correct ids I am trying to pass to the where_in()
}
$id = $query->result_array();
$this->db->where_in('id', $id); //pass array of 'id' from above query
$data = $this->db->get('Articles');
return $data->result_array();
}
If I alter the id array to contain only 1 value then the where_in() clause works fine and outputs the row of data matching the single 'id'.
如果我将 id 数组更改为仅包含 1 个值,则 where_in() 子句可以正常工作并输出与单个“id”匹配的数据行。
I've looked all over stack and google for help in using where_in() and I think I have everything correct or have tried a few different methods for passing an array correctly.
我已经在堆栈和谷歌上寻找了使用 where_in() 的帮助,我认为我一切都正确,或者尝试了几种不同的方法来正确传递数组。
Thanks for the help.
谢谢您的帮助。
EDIT:In the end I will be outputting this data with my controller using:
编辑:最后,我将使用我的控制器输出这些数据:
$this->output->set_output(json_encode($data));
For testing purposes I was just skipping the JSON and trying to output the data with PHP from the model directly.
出于测试目的,我只是跳过 JSON 并尝试直接从模型中使用 PHP 输出数据。
回答by Michael K
The array you try to pass is a multi dimensional array. Instead try this:
您尝试传递的数组是多维数组。而是试试这个:
$ids = array(); foreach ($query->result_array() as $id) { $ids[] = $id['id']; } $this->db->where_in('id', $ids);
You can not flatten the query->result_array() without iteration. But if you need to handle this kind of queries a lot in your application, and if you have >= PHP 5.3 installed, you could put the following function in a Codeigniter helper file (or somewhere else suitable) to help you flattening arrays:
您不能在没有迭代的情况下展平 query->result_array()。但是,如果您需要在应用程序中大量处理此类查询,并且安装了 >= PHP 5.3,则可以将以下函数放在 Codeigniter 帮助文件(或其他合适的地方)中以帮助您展平数组:
function flatten(array $array) { $return = array(); array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; }); return $return; }
And in your case use it like this:
在你的情况下,像这样使用它:
$ids = flatten($query->result_array()); $this->db->where_in('id', $ids);