【问题标题】:in_array multiple valuesin_array 多个值
【发布时间】:2011-11-24 10:55:47
【问题描述】:

如何检查多个值,例如:

$arg = array('foo','bar');

if(in_array('foo','bar',$arg))

这是一个例子,所以你可以更好地理解,我知道它不会起作用。

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    将目标与干草堆相交,并确保交点与目标精确相等:

    $haystack = array(...);
    
    $target = array('foo', 'bar');
    
    if(count(array_intersect($haystack, $target)) == count($target)){
        // all of $target is in $haystack
    }
    

    请注意,您只需验证生成的交集的大小与目标值数组的大小相同,即可说$haystack$target 的超集。

    要验证$target 中的至少一个值也在$haystack 中,您可以执行以下检查:

     if(count(array_intersect($haystack, $target)) > 0){
         // at least one of $target is in $haystack
     }
    

    【讨论】:

    • 这个解决方案不是最好的:$needles=array(1,2); $haystack=数组(1,1,2,3); return (count(array_intersect($haystack,$needles)) == count($needles));这将返回 false,这不好。 Rok Kralj 的解决方案非常适合这种情况。
    • 我认为这些数组应该保存唯一的数据,否则这个解决方案将不起作用
    【解决方案2】:

    作为开发人员,您可能应该开始学习集合操作(​​差、并、交)。您可以将您的数组想象为一个“集合”,而您正在搜索的键是另一个。

    检查是否所有针都存在

    function in_array_all($needles, $haystack) {
       return empty(array_diff($needles, $haystack));
    }
    
    echo in_array_all( [3,2,5], [5,8,3,1,2] ); // true, all 3, 2, 5 present
    echo in_array_all( [3,2,5,9], [5,8,3,1,2] ); // false, since 9 is not present
    

    检查是否有任何针存在

    function in_array_any($needles, $haystack) {
       return !empty(array_intersect($needles, $haystack));
    }
    
    echo in_array_any( [3,9], [5,8,3,1,2] ); // true, since 3 is present
    echo in_array_any( [4,9], [5,8,3,1,2] ); // false, neither 4 nor 9 is present
    

    【讨论】:

    • 注意:这种类型的数组声明是>= PHP 5.4
    • 出色的解决方案,您无需进行严格的比较。
    • 不要不必要地使用empty,它抑制错误报告!array_diff(..) 就可以了。
    【解决方案3】:
    if(in_array('foo',$arg) && in_array('bar',$arg)){
        //both of them are in $arg
    }
    
    if(in_array('foo',$arg) || in_array('bar',$arg)){
        //at least one of them are in $arg
    }
    

    【讨论】:

      【解决方案4】:

      离开@Rok Kralj 答案(最佳 IMO)来检查大海捞针是否存在,您可以使用 (bool) 而不是 !!,这有时会在代码审查期间造成混淆。

      function in_array_any($needles, $haystack) {
         return (bool)array_intersect($needles, $haystack);
      }
      
      echo in_array_any( array(3,9), array(5,8,3,1,2) ); // true, since 3 is present
      echo in_array_any( array(4,9), array(5,8,3,1,2) ); // false, neither 4 nor 9 is present
      

      https://glot.io/snippets/f7dhw4kmju

      【讨论】:

      • 这作为对 Rok 答案的评论比作为新答案更有用。
      • @Jaymin 怎么样(你提供了零细节)......对我来说非常好glot.io/snippets/f7dhw4kmju
      • @sMyles 我曾尝试在数组中使用字符串值而不是整数。你可以在你身边尝试一次,然后告诉我。
      • @Jaymin 仍然没有这个问题 glot.io/snippets/f7dhw4kmju
      • 这个 bool 转换在这里不适合,因为 array_intersect 返回一个可以为空或非空的数组。 @rok-kralj 解决方案是唯一首选的解决方案。
      【解决方案5】:

      恕我直言,Mark Elliot 的解决方案是解决此问题的最佳解决方案。如果您需要在数组元素之间进行更复杂的比较操作并且您使用的是 PHP 5.3,您可能还可以考虑以下内容:

      <?php
      
      // First Array To Compare
      $a1 = array('foo','bar','c');
      
      // Target Array
      $b1 = array('foo','bar');
      
      
      // Evaluation Function - we pass guard and target array
      $b=true;
      $test = function($x) use (&$b, $b1) {
              if (!in_array($x,$b1)) {
                      $b=false;
              }
      };
      
      
      // Actual Test on array (can be repeated with others, but guard 
      // needs to be initialized again, due to by reference assignment above)
      array_walk($a1, $test);
      var_dump($b);
      

      这依赖于闭包;比较功能可以变得更强大。 祝你好运!

      【讨论】:

      • 我改变了我所做的一切,所以在最终输出中我不需要这样做,虽然它总是很好学习,感谢您的投入和时间!
      【解决方案6】:
      if(empty(array_intersect([21,22,23,24], $check_with_this)) {
       print "Not found even a single element";
      } else {
       print "Found an element";
      }
      

      array_intersect() 返回一个数组,其中包含所有参数中存在的 array1 的所有值。请注意,密钥会被保留。

      返回一个数组,其中包含 array1 中的所有值,其值存在于所有参数中。


      empty() —判断一个变量是否为空

      如果 var 存在并且具有非空、非零值,则返回 FALSE。否则返回 TRUE。

      【讨论】:

        【解决方案7】:

        如果您只想通过比较检查来优化此搜索,我会这样做:

        public function items_in_array($needles, $haystack)
        {
            foreach ($needles as $needle) {
                if (!in_array($needle, $haystack)) {
                    return false;
                }
            }
        
            return true;
        }
        

        如果你愿意做多个if(in_array(...&amp;&amp;in_array(... 会更快。

        我使用 array_intersect、array_diff 和 in_array 对 100 个和另一个使用 100,000 个数组元素和 2 到 15 个针进行了测试。

        in_array 总是明显更快,即使我必须为不同的针做 15 倍。 array_diff 也比 array_intersect 快了很多。

        因此,如果您只是想搜索一些东西以查看它们是否存在于数组中,那么 in_array() 是最好的性能方面。如果您需要知道实际的差异/匹配,那么使用 array_diff/array_intersect 可能更容易。

        如果我在下面的丑陋示例中计算错误,请随时告诉我。

        <?php
        
        
        
        $n1 = rand(0,100000);
        $n2 = rand(0,100000);
        $n2 = rand(0,100000);
        $n3 = rand(0,100000);
        $n4 = rand(0,100000);
        $n5 = rand(0,100000);
        $n6 = rand(0,100000);
        $n7 = rand(0,100000);
        $n8 = rand(0,100000);
        $n9 = rand(0,100000);
        $n10 = rand(0,100000);
        $n11 = rand(0,100000);
        $n12 = rand(0,100000);
        $n13 = rand(0,100000);
        $n14 = rand(0,100000);
        $n15 = rand(0,100000);
        $narr = [$n1, $n2, $n3, $n4, $n5, $n6, $n7, $n8, $n9, $n10, $n11, $n12, $n13, $n14, $n15];
        
        $arr = [];
        for($i = 0; $i<100000 ; $i++)
        {
            
            $arr[] = rand(0,100000);
            
        }
        
        
        function array_in_array($needles, $haystack)
        {
            foreach($needles as $needle)
            {
            
                if (!in_array($needle, $haystack))
                    {
                        return false;
                    }
        
            }
            
            return true;
        }
        
        $start_time = microtime(true);
        $failed = true;
        if(array_in_array($narr, $arr))
        {
            echo "<br>true0<br>";
        }
        
        $total_time = microtime(true) - $start_time;
        echo "<hr>";
        echo($total_time);
        
        
        
        $start_time = microtime(true);
        
        if (
            in_array($n1, $arr) !== false &&
            in_array($n2, $arr) !== false &&
            in_array($n3, $arr) !== false &&
            in_array($n4, $arr) !== false &&
            in_array($n5, $arr) !== false &&
            in_array($n6, $arr) !== false &&
            in_array($n7, $arr) !== false &&
            in_array($n8, $arr) !== false &&
            in_array($n9, $arr) !== false &&
            in_array($n10, $arr) !== false &&
            in_array($n11, $arr) !== false &&
            in_array($n12, $arr) !== false &&
            in_array($n13, $arr) !== false &&
            in_array($n14, $arr) !== false &&
            in_array($n15, $arr)!== false) {
            echo "<br>true1<br>";
        }
        
        $total_time = microtime(true) - $start_time;
        echo "<hr>";
        echo($total_time);
        
        $first_time = $total_time;
        
        echo "<hr>";
        
        $start_time = microtime(true);
        
        if (empty($diff = array_diff($narr,$arr)))
        {
        
            echo "<br>true2<br>";
        }
        
        
        $total_time = microtime(true) - $start_time;
        echo($total_time);
        print_r($diff);
        echo "<hr>";
        
        echo "<hr>";
        if ($first_time > $total_time)
        {
            echo "First time was slower";
        }
        
        if ($first_time < $total_time)
        {
            echo "First time was faster";
        }
        
        echo "<hr>";
        
        
        $start_time = microtime(true);
        
        if (count(($itrs = array_intersect($narr,$arr))) == count($narr))
        {
        
            echo "<br>true3<br>";
            print_r($result);
        }
        
        
        $total_time = microtime(true) - $start_time;
        echo "<hr>";
        echo($total_time);
        
        print_r($itrs);
        
        echo "<hr>";
        
        if ($first_time < $total_time)
        {
            echo "First time was faster";
        }
        
        
        echo "<hr>";
        print_r($narr);
        echo "<hr>";
        print_r($arr);
        

        【讨论】:

          猜你喜欢
          • 2013-03-30
          • 2014-07-26
          • 1970-01-01
          • 1970-01-01
          • 2011-05-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多