douxing2652 2016-11-02 01:34
浏览 18
已采纳

编写并保存在变量中时字符串不同

Okay, so I have this confusing problem with a string. I'm using a loop to construct the string, and I use print_r($exclude) to see the result. The output of the string is:

101,102,135

... which is correct. Then I'm trying to use $exclude in an argument-array for a WordPress-query:

'terms' => array($exclude),

In short this should exclude posts from the categories with ID's mentioned above. But this does not work as intended. Unless if I write the numbers directly like this, it works:

'terms' => array(101,102,135),

So what is the difference between the $exclude-string and writing the numbers manually...!?

  • 写回答

3条回答 默认 最新

  • douchong4730 2016-11-02 01:42
    关注

    When you do this

    'terms' => array($exclude)
    

    Your terms array looks like this:

    Array
    (
        [0] => 101,102,135
    )
    

    Solution

    'terms' => explode(',', $exclude)
    

    $exclude becomes

    Array
    (
        [0] => 101
        [1] => 102
        [2] => 135
    )
    

    You are assigning a string to the 0-th key in array, whereas you should be passing array elements.

    explode will split up your string into an array based on a passed delimiter (in your case a comma).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部