简体   繁体   中英

remove duplicates from a group of arrays

I have the following group of arrays in one array, where the content of them is a group of posts :

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" => []
]

The arrays :

    $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;

There might be some posts repeated in more than one array. I tried to use array_unique($Final_updts); , but received Error exception: Array to string conversion

Is there any easy way to do this, or I have to modify the 5 queries to prevent any duplicate results ?

I assume that you want to get unique items by id. Lets use collection to solve this problem.

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

Pls let me know if I got it right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM