【问题标题】:Is it possible to check if a value exists inside an array full of objects without looping?是否可以在不循环的情况下检查一个值是否存在于一个充满对象的数组中?
【发布时间】:2023-04-03 17:12:01
【问题描述】:

我有一个包含多个对象的数组。是否可以检查任何一个对象中是否存在值,例如id->27 没有循环?与 PHP 的 in_array() 函数类似。谢谢。

> array(10)[0]=>Object #673 
                     ["id"]=>25 
                     ["name"]=>spiderman   
           [1]=>Object #674
                     ["id"]=>26
                     ["name"]=>superman   
           [2]=>Object #675
                     ["id"]=>27
                     ["name"]=>superman 
           ....... 
           .......
           .........

【问题讨论】:

    标签: php arrays oop


    【解决方案1】:

    没有。如果您经常需要快速直接查找值,则需要为它们使用数组键,查找速度快如闪电。例如:

    // prepare once
    $indexed = array();
    foreach ($array as $object) {
        $indexed[$object->id] = $object;
    }
    
    // lookup often
    if (isset($indexed[42])) {
        // object with id 42 exists...
    }
    

    如果您需要通过不同的键来查找对象,因此您不能真正通过一个特定的键来索引它们,您需要研究不同的搜索策略,例如binary searches

    【讨论】:

      【解决方案2】:
      $results = array_filter($array, function($item){
         return ($item->id === 27);
      });
      if ($results)
      {
         ..  You have matches
      }
      

      【讨论】:

      • 当你的意思是$var时不要使用!empty($var)
      【解决方案3】:

      您将需要以一种或另一种方式循环 - 但您不必自己手动实现循环。看看array_filter function。您需要做的就是提供一个检查对象的函数,如下所示:

      function checkID($var)
      {
          return $var->id == 27;
      }
      
      if(count(array_filter($input_array, "checkID")) {
          // you have at least one matching element
      }
      

      或者你甚至可以在一行中做到这一点:

      if(count(array_filter($input_array, function($var) { return $var->id == 27; })) {
          // you have at least one matching element
      }
      

      【讨论】:

      • 这可能非常浪费,因为一旦找到值它就不会中断,而是总是遍历所有值。
      • @deceze 同意。问题是他的阵列有多大。如果他处理数以百万计的项目,这将是不好的。如果数组只包含几个项目(甚至几百个),则影响可以忽略不计。
      • empty 对函数返回值不起作用,反正这里也没必要用。纯布尔比较就可以了。
      • @deceze 我知道。我不确定那里发生了什么:我之前编辑过我的帖子,删除了empty - 但它重新出现了。我又编辑了。
      【解决方案4】:

      您可能希望结合使用两个函数来获得所需的结果。

      array_search($needle, array_column($array, 'key_field');

      创建了一个小代码来演示它的使用。

      <?php
      $superheroes    =    [
          [
              "id"    =>    1,
              "name"  =>    "spiderman"
          ],
          [
              "id"    =>    2,
              "name"  =>    "superman"
          ],
          [
              "id"    =>    3,
              "name"  =>    "batman"
          ],
          [
              "id"    =>    4,
              "name"  =>    "robin"
          ],
      ];
      $needle    =    'spiderman';
      $index     =    array_search($needle, array_column($superheroes, "name"));
      echo "Is $needle a superhero?<br/>";
      
      
      //Comparing it like this is important because if the element is found at index 0, 
      //array_search will return 0 which means false. Hence compare it with !== operator
      if ( false !== $index ) {
          echo "yes";
      } else {
          echo "no";
      }
      ?>
      

      【讨论】:

        【解决方案5】:

        你可以这样做:

        foreach ($array as $value)
        {
           if ($value == "what you are looking for")
               break;
        }
        

        【讨论】:

        • 你说没有遍历整个数组
        • 哈哈是真的。我会适当改变。
        【解决方案6】:

        array_search — 在数组中搜索给定值并返回 成功对应的key

        $key = array_search('your search', $array);
        

        【讨论】:

        • 虽然这是一个很好的功能,但它不适用于 OP,因为他需要检查数组值中的字段,而不是值本身 - 而array_search 不接受比较函数
        猜你喜欢
        • 1970-01-01
        • 2019-11-26
        • 1970-01-01
        • 2018-06-27
        • 2017-09-17
        • 2014-10-28
        • 2021-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多