本文介绍了ActiveRecord where_in()与数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经尝试了几种解决该问题的方法,但没有解决方案.我收到此错误消息:

I have tried a few different approaches to this problem with no solutions so far. I am receiving this error message:

"where子句"中的未知列"Array"

Unknown column 'Array' in 'where clause'

SELECT * FROM(Articles)在哪里id IN(数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组,数组)

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)

这是我的模型代码:

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();
}

如果我将id数组更改为仅包含1个值,则where_in()子句可以正常工作,并输出与单个"id"匹配的数据行.

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'.

在使用where_in()的过程中,我已经遍历整个堆栈和Google寻求帮助,我认为我的所有设置都正确无误,或者尝试了几种不同的方法来正确传递数组.

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.

感谢您的帮助.

编辑:最后,我将使用以下命令与控制器一起输出此数据:

In the end I will be outputting this data with my controller using:

$this->output->set_output(json_encode($data));

出于测试目的,我只是跳过JSON并尝试直接从模型中使用PHP输出数据.

For testing purposes I was just skipping the JSON and trying to output the data with PHP from the model directly.

推荐答案

您尝试传递的数组是多维数组.而是尝试以下方法:

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);

如果不进行迭代,则无法展平query-> result_array().但是,如果您需要在应用程序中处理大量此类查询,并且已安装> = PHP 5.3,则可以将以下函数放在Codeigniter帮助程序文件(或其他合适的位置)中,以帮助您展平数组: /p>

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:


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);

这篇关于ActiveRecord where_in()与数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:45