【问题标题】:How to Remove value from an array using another array with multiple values?如何使用具有多个值的另一个数组从数组中删除值?
【发布时间】:2018-12-24 07:16:16
【问题描述】:

我有两个数组 Array1 和 Array2,我需要从 Array1 中删除 Array2 值我在这里显示我的两个数组。

在 Array1 中,我的 utype_id 是 11 和 14,我需要从 Array2 中删除这个 id 记录,所以我该怎么做,你能帮帮我吗?

Array1(

    [0] => stdClass Object
        (
            [id] => 22
            [accessid] => 2
            [utype_id] => 11
            [discount] => 3434
            [published] => 1
        )

    [1] => stdClass Object
        (
            [id] => 23
            [accessid] => 2
            [utype_id] => 14
            [discount] => 2
            [published] => 1
        )
)


Array2
(
    [0] => stdClass Object
        (
            [id] => 9
            [type_name] => Admin
            [description] => admin
            [published] => 0
        )

    [1] => stdClass Object
        (
            [id] => 10
            [type_name] => Senior sales
            [description] => senior sales
            [published] => 0
        )

    [2] => stdClass Object
        (
            [id] => 11
            [type_name] => junior sales
            [description] => junior
            [published] => 1
        )

    [3] => stdClass Object
        (
            [id] => 14
            [type_name] => dealer
            [description] => dealer
            [published] => 0
        )

    [4] => stdClass Object
        (
            [id] => 15
            [type_name] => fgdg
            [description] => dfg
            [published] => 1
        )

    [5] => stdClass Object
        (
            [id] => 16
            [type_name] => fgdfg
            [description] => fgdfg
            [published] => 0
        )

)

我没有得到任何解决方案。我只需要 Array2 中的 9,10,15,16 记录 id。

【问题讨论】:

  • 请显示您到目前为止尝试了什么。
  • $diff = array_diff($array2, $array1);
  • 一个数组中的所有项都不在另一个数组中
  • 从第一个数组中提取值,然后根据提取的值迭代或过滤第二个数组。
  • 对象数组使其更难,没有array_column :(

标签: php arrays


【解决方案1】:

仅出于娱乐目的(我感觉有点被忽视了:()。按 ID 索引两个数组(需要 php 7+ 才能将 array_column() 支持对象作为输入),然后 array_diff_key() 删除任何第二个数组...

print_r(array_diff_key(array_column($array2, null, "id"), 
                  array_column($array1, null, "utype_id")));

我想说foreach()的解决方案比这个快,只是想加入并发布一些原创内容。

【讨论】:

    【解决方案2】:

    首先,从第一个数组中提取utype_ids,使其成为keys以加快搜索速度:

    $utype_ids = [];
    foreach ($array1 as $item) {
        $utype_ids[$item->utype_id] = 1;
    }
    

    然后,使用$utype_ids 过滤第二个数组:

    $filtered_array = array_filter(
        $array2, 
        function($v) use ($utype_ids) {
            return !isset($utype_ids[$v->id]);
        }
    );
    

    演示:https://3v4l.org/i2heV

    【讨论】:

      【解决方案3】:

      使用嵌套循环执行限定检查。最佳做法是使用break,以避免不必要的迭代。

      代码:(Demo)

      $blacklist = [
          (object)["id" => 22,"accessid" => 2, "utype_id" => 11, "discount" => 3434, "published" => 1],
          (object)["id" => 23,"accessid" => 2, "utype_id" => 14, "discount" => 2, "published" => 1]
      ];
      
      $rows = [
          (object)["id" => 9, "type_name" => "Admin", "description" => "admin", "published" => 0],
          (object)["id" => 10, "type_name" => "Senior sales", "description" => "senior sales", "published" => 0],
          (object)["id" => 11, "type_name" => "junior sales", "description" => "junior sales", "published" => 1],
          (object)["id" => 14, "type_name" => "dealer", "description" => "dealer", "published" => 0],
          (object)["id" => 15, "type_name" => "fgdg", "description" => "dfg", "published" => 1],
          (object)["id" => 16, "type_name" => "fgdfg", "description" => "fgdfg", "published" => 0]
      ];
      
      foreach ($blacklist as $disqualifier) {                 // iterate the blacklist
          foreach ($rows as $index => $row) {                 // iterate the list to be checked
              if ($row->id === $disqualifier->utype_id) {     // if row should be disqualified
                  unset($rows[$index]);                       // remove the row
                  break;                                      // stop checking the $rows for this $disqualifier
              }
          }
      }
      
      var_export($rows);
      

      ...如果您需要重新索引输出,您可以致电array_values($rows)

      如果这些对象数组来自数据库表,您应该改进查询以提前执行此过滤过程。

      【讨论】:

        【解决方案4】:

        你可以使用..

        $arr1ids = array();
        foreach($array1 as $val1){
            $arr1ids[] = $val1->utype_id;
        }
        
        $resArr = array();
        foreach($array2 as $val2){
            if(!in_array($val2->utype_id,$arr1ids)){
               $resArr[] = $val2;
            }  
        }
        
        print_r($resArr);
        

        【讨论】:

        • 仅代码答案在 StackOverflow 上的价值很低
        猜你喜欢
        • 1970-01-01
        • 2013-02-19
        • 1970-01-01
        • 2016-07-26
        • 2019-05-30
        • 2021-08-06
        • 1970-01-01
        • 2016-08-05
        • 1970-01-01
        相关资源
        最近更新 更多