从一组数组中删除重复项

时间:2019-12-07 14:47:35

标签: php laravel

我在一个数组中有以下一组数组,其中的内容是一组帖子:

array:5 [▼
  "in" => array:2 [▼
    0 => {#371 ▼
      +"id": 61
      +"created_at": "2019-11-29 08:12:11"
      +"updated_at": "2019-11-29 08:12:11"
      +"title": "Sports test "
      +"body": "Test answer"
      +"ttype": 0
      +"cat": 0
      +"a_id": 61
      +"tag": ""
    }
    1 => {#372 ▶}
  ]
  "out" => []
  "other" => []
  "fol" => array:17 [▶]
  "tag" => []
]

数组:

    $Final_updts['in'] = $all_update_in;
     $Final_updts['out'] = $all_update_out;
     $Final_updts['other'] = $all_update_oth;
     $Final_updts['fol'] = $follow_psts;
     $Final_updts['tag'] = $post_tags;

有些帖子可能会在多个数组中重复出现。我尝试使用array_unique($Final_updts);,但收到了Error exception: Array to string conversion

是否有任何简单的方法来执行此操作,或者我必须修改5个查询以防止出现重复的结果?

1 个答案:

答案 0 :(得分:0)

我假设您想通过ID获得唯一的商品。让我们使用collection解决此问题。

$results = collect($array)->collapse()->unique('id');

请让我知道我做对了。