douxing2652 2016-11-02 09: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 09: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条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测