数组字符串转换错误 [英] Array to string conversion error

查看:131
本文介绍了数组字符串转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拉动的ID列表查询。这些ID是一个数组,我需要寻找另一个表与标识。我试着用破灭,使这些ID,我可以在where子句中使用一个字符串,但我不断收到此错误。

我目前的code是:

  $查询= $这个 - > DB-GT&;查询('
        选择 *
        从system_scoperights
        其中user ='。 $这个 - >会话级>用户数据(用户名)。
    ');    的foreach($查询 - >的结果()为$行){
        $范围= $行向>网站;
        $数据[] = $范围;
    }
    $ DATASCOPE [] = $的数据;    $ IDLIST =破灭('',$ Datascope公司); < ----错误行    $哪里='WHERE scope_scopes.sc_ID IN。 $ IDLIST'。

我已经尝试了不同的东西,我在论坛上发现这样的:

  $ IDLIST =破灭('',array_values​​($ Datascope公司));

  $ IDLIST =破灭('',加入($ Datascope公司));

但这些都不工作。 (我从来没有听说过的连接功能)

在此先感谢您的帮助。


解决方案

  $ DATASCOPE [] = $的数据;

  $数据[] = $范围;

因此​​, $ Datascope公司有它的内部数组的数组。 破灭仅在一个水平上工作,所以就是为什么你收到此错误。

您应该注意,这实际上是可能在SQL:

  SELECT * FROM some_table WHERE ID IN(SELECT的网站从another_table WHERE ...)

这将消除此code。整个需求。

这就是:

  $,其中='WHERE scope_scopes.sc_ID IN(SELECT网站
                                       从system_scoperights
                                       其中user ='。 $这个 - >会话级>用户数据(用户名)。 ')';

I have a query that's pulling a list of IDs. Those IDs are in an array and I need to search another table with those IDs. I tried using implode to make those IDs a string that I could use in a where clause but I keep getting this error.

My current code is:

 $query = $this->db->query('
        SELECT *
        FROM system_scoperights
        WHERE user = '. $this->session->userdata('username') .'
    ');

    foreach ($query->result() as $row) {
        $scope = $row->site;
        $data[] = $scope;
    } 
    $dataScope[] = $data;

    $idList = implode(',', $dataScope);   <---- Error Line

    $where = 'WHERE scope_scopes.sc_ID IN '. $idList .'';

I've tried different things I found on forums like:

 $idList = implode(',', array_values($dataScope));

and

 $idList = implode(',', join($dataScope));

but none of those work. (I've never even heard of the join function)

Thanks in advance for the help.

解决方案

$dataScope[] = $data;

but

$data[] = $scope;

therefore $dataScope has an array inside it's array. implode only work on one level, so that why you're getting this error.

You should note that this is actually possible in SQL:

 SELECT * FROM some_table WHERE id IN (SELECT site FROM another_table WHERE ... )

which would eliminate the entire need for this code.

That is:

$where = 'WHERE scope_scopes.sc_ID IN (SELECT site
                                       FROM system_scoperights
                                       WHERE user = '. $this->session->userdata('username') . ')';

这篇关于数组字符串转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆