douhan5853 2014-11-21 08:43
浏览 27
已采纳

php notContainedIn不识别数组参数

I am running into a problem where the below is not working as I think it should.

// this will not work and $usernames appears empty
$usernames = "\"user1\", \"user2\"";
$query->notContainedIn("username", [$usernames]);



// this does work
$user1 = "user1";
$user2 = "user2";
$query->notContainedIn("username", ["$user1", "$user2"]);

Thanks

  • 写回答

1条回答 默认 最新

  • dth981485742 2014-11-21 10:08
    关注

    According to https://parse.com/docs/php_guide#queries you need to provide an array and $usernames is a string. By using the short-array notation, i.e. the square brackets, you've created an array with precisely one element, namely the string. What you need is an array with two elements corresponding to the two values of interest.

    If for some reason your data is only available in this type of formatted string, then you would need to convert it to an array. There are at least two ways you could do this:

    <?php
    $usernames = explode(",",str_replace("\"","","\"user1\",\"user2\""));
    ?>
    

    This is less complicated than it might appear. The code replaces the quotes encompassing each value with empty strings. Then it explodes the string in two by indicating that the comma is a delimitor. Although I've never worked with Parse, I would expect that you could replace the second parameter in the query with $usernames now, as follows:

    <?php
    $query->notContainedIn("username", $usernames); 
    

    Don't use the square brackets or you'll be creating a multidimensional array whose first element contains the array $usernames.

    Alternative way of converting said string to array:

    <?php
    $string = "\"user1\",\"user2\"";
    $tok = strtok($string, "\",");
    
    $usernames = null;
    while ($tok !== false) {
        $usernames[] = "$tok";
        $tok = strtok("\",");
    }
    

    $usernames now contains two values just like in the previous solution I proposed. While this second solution is more involved, note that it uses only one function instead of two, so it might produce faster results. You could try both solutions, benchmark them, and then pick the one you find to have superior performance.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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