doumo0206 2016-11-11 00:38
浏览 98
已采纳

如何检查array_intersect的结果是否为空?

I am trying to check the result of an array_intersect as a regular array but it always returns true even if the array is empty.

      $lecturedayssplit1 = preg_split("/(?=[A-Z])/", "TF");
      $lecturedayssplit2 = preg_split("/(?=[A-Z])/", "MTh");
      $lectolec=array_intersect($lecturedayssplit1,$lecturedayssplit2);
        if (count($lectolec) > 0) {
            echo "Yeah!!!";
            print_r($lectolec);
        } else {
            echo "Nooo!";
            print_r($lectolec);
        }

$lectolec in the problem shouldn't return a count greater than 0 because there is no intersection between the two arrays. I also tried if (empty($lectolec)) but it also didn't work. Hopefully someone can help out. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dtlab08822 2016-11-11 00:55
    关注

    Because you are using a "zero-width positive lookahead" (?=[A-Z]) regex, it is matching an empty sequence in the beginning of each test. Therefore, array_intersect matches that empty sequence in both.

    You can use the flag PREG_SPLIT_NO_EMPTY to ignore those empty results.

    $lecturedayssplit1 = preg_split("/(?=[A-Z])/", "TF", -1, PREG_SPLIT_NO_EMPTY);
    $lecturedayssplit2 = preg_split("/(?=[A-Z])/", "MTh", -1, PREG_SPLIT_NO_EMPTY);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿