How to Remove Duplicate Values from an Array in PHP

How to remove duplicate values from an array in PHP

Use array_unique().

Example:

$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)

How to remove duplicate values from a multi-dimensional array in PHP

Here is another way. No intermediate variables are saved.

We used this to de-duplicate results from a variety of overlapping queries.

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

how to remove duplicate elements from an array of arrays in PHP

Just use array_map and array_unique together.

<?php
$a = json_decode('{"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"],
"2017-08-22":["5948a0dd21146a43fdcfef5a"],
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"],
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]}', true);

echo json_encode(array_map("array_unique", $a));

Remove duplicate items from an array

The array_unique function will do this for you. You just needed to add the SORT_REGULAR flag:

$items_thread = array_unique($items_thread, SORT_REGULAR);

However, as bren suggests, you should do this in SQL if possible.



Related Topics



Leave a reply



Submit