How to remove duplicate values from an array in PHP
Asked Answered
A

22

248

How can I remove duplicate values from an array in PHP?

Abana answered 21/11, 2008 at 2:37 Comment(2)
It depends on context. Do you have a flat array or a multidimensional array? Are all of your values scalar? Might your values contain float/double values? Do you want to keep the first or last occurring duplicates? Do you need to reindex the array? (nimey sara thomas seems to think this is a pertinent step for some odd reason) Where is your sample input? What is your exact desired result? minimal reproducible examples make the best questions. This question needs clarity to avoid receiving answers that are unsuitable or even wrong.Unnumbered
What type of data is in the array? Is it direct data or more arrays? If direct data, what kind/type of data? The answer depends heavily on that information.Narah
E
385

Use array_unique() for a one-dimensional array. From the PHP manual:

Takes an input array and returns a new array without duplicate values.

Note that keys are preserved. If multiple elements compare equal under the given flags, then the key and value of the first equal element will be retained.

Note that array_unique() is not intended to work on multi dimensional arrays.

Example:

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

If you want the values re-indexed, in addition, you should apply array_values.

Epizoic answered 21/11, 2008 at 2:39 Comment(0)
S
88

Use array_values(array_unique($array));

array_unique: for unique array array_values: for reindexing

Skip answered 15/2, 2017 at 13:49 Comment(2)
+1 array_unique returns an object with key and value pairs AND array_values return only values as an array.Diamonddiamondback
@Diamonddiamondback (and the 8 comment upvoters)... What?!? array_unique() DEMANDS an array as the first parameter. And array_values() DEMANDS an array as its parameter. If you are dealing with an object, then this code will break. Please do not mislead researchers with untruths.Unnumbered
T
35
//Find duplicates 

$arr = array( 
    'unique', 
    'duplicate', 
    'distinct', 
    'justone', 
    'three3', 
    'duplicate', 
    'three3', 
    'three3', 
    'onlyone' 
);

$unique = array_unique($arr); 
$dupes = array_diff_key( $arr, $unique ); 
    // array( 5=>'duplicate', 6=>'three3' 7=>'three3' )

// count duplicates

array_count_values($dupes); // array( 'duplicate'=>1, 'three3'=>2 )
Thermosetting answered 6/12, 2011 at 11:23 Comment(0)
C
21
$result = array();
foreach ($array as $key => $value){
  if(!in_array($value, $result))
    $result[$key]=$value;
}
Cyclothymia answered 12/2, 2018 at 21:5 Comment(0)
S
18

The only thing which worked for me is:

$array = array_unique($array, SORT_REGULAR);

Edit : SORT_REGULAR keeps the same order of the original array.

Sikorsky answered 12/1, 2017 at 18:57 Comment(0)
M
6

sometimes array_unique() is not the way, if you want get unique AND duplicated items...

$unique=array("","A1","","A2","","A1","");
$duplicated=array();

foreach($unique as $k=>$v) {

if( ($kt=array_search($v,$unique))!==false and $k!=$kt )
 { unset($unique[$kt]);  $duplicated[]=$v; }

}

sort($unique); // optional
sort($duplicated); // optional

results on

array ( 0 => '', 1 => 'A1', 2 => 'A2', ) /* $unique */

array ( 0 => '', 1 => '', 2 => '', 3 => 'A1', ) /* $duplicated */
Mead answered 13/5, 2011 at 22:57 Comment(1)
This answer uses N number of searches while looping -- not very efficient. This answer makes loose comparisons which means that it may not be safe for general/public use. ProofUnnumbered
P
4
$a = array(1, 2, 3, 4); 
$b = array(1, 6, 5, 2, 9); 
$c = array_merge($a, $b);
$unique = array_keys(array_flip($c));
print_r($unique);
Poker answered 10/4, 2019 at 12:38 Comment(6)
The quickest way to achieve this is to use the array_flip function built-in to PHP[1]. array_flip will swap the array values with their keys and since an array cannot have duplicate keys you will end up with a unique set of keys that correspond to the values of the original array. To retrieve these keys as values you can use the array_keys function to retrieve your unique values. Both array_flip and array_keys are worst-case O(n) functions while array_unique has a worst-case of O(n log(n)).[2]Poker
Please add some more explanation to your answer (not to the comment section!). How does the given code remove duplicate values from a single array? Why do you need two arrays for that?Shari
Welcome to StackOverflow! I see that you have added some explanation in the comments of your answer, it would be helpful if you add this information as part of your answer itself.Plasty
Seems more reasonable to add that comment as an edit to already long-existing answer (https://mcmap.net/q/116007/-how-to-remove-duplicate-values-from-an-array-in-php).Daybreak
You can use single array with duplicate elements. I had problem of getting values from two arrays into one then remove duplicates.Poker
Calling array_flip() is not stable/reliable for general use. Values that coalesce to the same string or integer value will be consolidated/lost despite not being identical. Also, since PHP4.3 (maybe earlier, I don't know), this solution will emit Warnings when values are not strings or integers. Proof I do not endorse this answer because it contains no explanatory text and is unsafe to use in some scenariosUnnumbered
P
4

We can easily use arrar_unique($array); to remove duplicate elements But the problem in this method is that the index of the elements are not in order, will cause problems if used somewhere else later.

Use

$arr = array_unique($arr);
$arr = array_values($arr);
print_r($arr);

Or

$arr = array_flip($arr);
$arr = array_flip($arr);
$arr = array_values($arr);
print_r($arr);

The first flip , flips the key value pair thus combines the elements with similar key(that was originally the value).

2nd flip to revert all the key value pairs. Finally array_value() sets each value with key starting from 0.

Note: Not to be used in associative array with predefined key value pairs

Philan answered 7/7, 2021 at 20:53 Comment(1)
Calling array_flip() is not stable/reliable for general use. Values that coalesce to the same string or integer value will be consolidated/lost despite not being identical. Also, since PHP4.3 (maybe earlier, I don't know), this solution will emit Warnings when values are not strings or integers. Proof The advice to use array_unique() was provided a decade earlier on this page. I do not recommend this answer -- it either gives redundant or unstable advice.Unnumbered
H
3

We can create such type of array to use this last value will be updated into column or key value and we will get unique value from the array...

$array = array (1,3,4,2,1,7,4,9,7,5,9);
    $data=array();
    foreach($array as $value ){

        $data[$value]= $value;

    }

    array_keys($data);
    OR
    array_values($data);
Hereupon answered 29/6, 2017 at 8:55 Comment(1)
Using values as keys is not stable when values are mutated by PHP upon being used as keys. This answer may not be suitable for public/general use.Unnumbered
W
2

Depending on the size of your array, I have found

$array = array_values( array_flip( array_flip( $array ) ) );

can be faster than array_unique.

Whack answered 25/7, 2016 at 1:57 Comment(4)
Any more information on what's going on here and would it be faster with a bigger or smaller array.Lindsay
The double flip is going to remove duplicated values, because a key can't exist twice, otherwise it gets overwritten. If any value is duplicated and the array is flipped, the last occurrence (I assume) will be the value for the key.Cholon
In PHP 7 I've noticed flipping a multidimensional array more than once may reorder array elements unexpectedly.Discard
Calling array_flip() is not stable/reliable for general use. Values that coalesce to the same string or integer value will be consolidated/lost despite not being identical. Also, since PHP4.3 (maybe earlier, I don't know), this solution will emit Warnings when values are not strings or integers. ProofUnnumbered
A
2

If your concern is performance and you have a simple array, use:

array_keys(array_flip($array));

It's many times faster than array_unique.

Apiece answered 1/10, 2018 at 12:59 Comment(1)
While it may be faster, it is also vulnerable to breakage because if non-string or non-integer values are used as keys, there may be unwanted collisions based on what PHP will use as the new keys.Unnumbered
P
2

This example is just an alternative.

    <?php
    $numbers = [1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4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4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1,1,3,4,5,6,2,5,7,1];
    $unique_numbers = [];
    
    foreach($numbers as $number)
    {
      if(!in_array($number,$unique_numbers)){
          $unique_numbers[] = $number;
      }
    }
    print(json_encode($unique_numbers)); //// Array is now 1,3,4,5,6,2,7, ....
Paracasein answered 2/8, 2022 at 5:28 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Anemia
https://mcmap.net/q/116007/-how-to-remove-duplicate-values-from-an-array-in-php already demonstrated the use of !in_array() years earlier. in_array() is performing loose comparisons in this snippet and may not be fit for general/public use.Unnumbered
B
1

explode(",", implode(",", array_unique(explode(",", $YOUR_ARRAY))));

This will take care of key associations and serialize the keys for the resulting new array :-)

Bille answered 18/8, 2010 at 12:41 Comment(3)
What would be the effect of explode("," $YOUR_ARRAY) when $YOUR_ARRAY is array("1", "2", "3")?Twig
@kiamlaluno - :) and the answer is: Warning: explode() expects parameter 2 to be string, array given in ... on line ....Wacky
@JesseChisholm Yes, this was the point, but nobody noticed the code, apparently.Twig
M
1

Remove duplicate values from an associative array in PHP.

$arrDup = Array ('0' => 'aaa-aaa' , 'SKU' => 'aaa-aaa' , '1' => '12/1/1' , 'date' => '12/1/1' , '2' => '1.15' , 'cost' => '1.15' );

foreach($arrDup as $k =>  $v){
  if(!( isset ($hold[$v])))
      $hold[$v]=1;
  else
      unset($arrDup[$k]);
}

Array ( [0] => aaa-aaa [1] => 12/1/1 [2] => 1.15 )

Mcneal answered 12/2, 2018 at 20:39 Comment(1)
Using values as keys makes this technique unreliable for general use. When non-string or non-integer values are used as keys, they will either be silently converted to a string/integer or PHP will emit a Warning.Unnumbered
P
1

There can be multiple ways to do these, which are as follows

//first method
$filter = array_map("unserialize", array_unique(array_map("serialize", $arr)));

//second method
$array = array_unique($arr, SORT_REGULAR);
Proprietary answered 5/6, 2018 at 8:33 Comment(1)
It might be worth mentioning that this unexplained answer is intended for multidimensional arrays and loops over the input array several times to do its job. array_unique() with SORT_REGULAR cannot be trusted with empty strings, false and null values -- ProofUnnumbered
J
0

That's a great way to do it. Might want to make sure its output is back an array again. Now you're only showing the last unique value.

Try this:

$arrDuplicate = array ("","",1,3,"",5);

foreach (array_unique($arrDuplicate) as $v){
  if($v != "") { $arrRemoved[] = $v; }
}
print_r ($arrRemoved);
Jackfruit answered 9/8, 2012 at 9:53 Comment(1)
Why did you bother to correct the low-value answer? The question does not ask to populate an array of removed elements. Also there is no requirement to explicitly collect non-empty strings in a new array. The array_unique() advice has already been given. This answer adds no new, relevant advice to this page.Unnumbered
B
0

try this short & sweet code -

$array = array (1,4,2,1,7,4,9,7,5,9);
$unique = array();

foreach($array as $v){
  isset($k[$v]) || ($k[$v]=1) && $unique[] = $v;
  }

var_dump($unique);

Output -

array(6) {
  [0]=>
  int(1)
  [1]=>
  int(4)
  [2]=>
  int(2)
  [3]=>
  int(7)
  [4]=>
  int(9)
  [5]=>
  int(5)
}
Behling answered 1/4, 2014 at 9:47 Comment(3)
Abusing boolean operators for control flow like this is needlessly confusing. Just use if.Bascom
you are not getting making key but checking with key valueFluorite
This solution is not fit for general use because when values are converted to string/integer keys some values are incorrectly deemed "the same". ProofUnnumbered
C
0

It can be done through function I made three function duplicate returns the values which are duplicate in array.

Second function single return only those values which are single mean not repeated in array and third and full function return all values but not duplicated if any value is duplicated it convert it to single;

function duplicate($arr) {
    $duplicate;
    $count = array_count_values($arr);
    foreach($arr as $key => $value) {
        if ($count[$value] > 1) {
            $duplicate[$value] = $value;
        }
    }
    return $duplicate;
}
function single($arr) {
    $single;
    $count = array_count_values($arr);
    foreach($arr as $key => $value) {
        if ($count[$value] == 1) {
            $single[$value] = $value;
        }
    }
    return $single;
}
function full($arr, $arry) {
    $full = $arr + $arry;
    sort($full);
    return $full;
}
Camargo answered 27/7, 2015 at 6:45 Comment(1)
Calling array_count_values() bears the same potential vulnerability as array_flip() -- problems can occur when values are converted to string/integer keys.Unnumbered
G
0
<?php
$arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];    
print_r(arr_unique($arr1));


function arr_unique($arr) {
  sort($arr);
  $curr = $arr[0];
  $uni_arr[] = $arr[0];
  for($i=0; $i<count($arr);$i++){
      if($curr != $arr[$i]) {
        $uni_arr[] = $arr[$i];
        $curr = $arr[$i];
      }
  }
  return $uni_arr;
}
Grum answered 24/1, 2017 at 8:35 Comment(2)
Putting count($arr) in the loop is quite slowNewfangled
I don't know that calling count() in the second parameter of the loop is slow, but it is certainly unnecessary because the count is never affected by the loop's body. This answer is not reliable for general use because loose comparisons result in unintended (non-unique) value eliminations. Proof (This answer is also missing its educational explanation.Unnumbered
M
0
    if (@!in_array($classified->category,$arr)){        
                                    $arr[] = $classified->category;
                                 ?>

            <?php } endwhile; wp_reset_query(); ?>

first time check value in array and found same value ignore it

Mezcaline answered 15/6, 2017 at 5:42 Comment(1)
This answer does not run, is poorly formatted, should not endorse error suppression, is performing loose comparisons, and is missing its educational explanation.Unnumbered
O
0

Here I've created a second empty array and used for loop with the first array which is having duplicates. It will run as many time as the count of the first array. Then compared with the position of the array with the first array and matched that it has this item already or not by using in_array. If not then it'll add that item to second array with array_push.

$a = array(1,2,3,1,3,4,5);
$count = count($a);
$b = [];
for($i=0; $i<$count; $i++){
    if(!in_array($a[$i], $b)){
        array_push($b, $a[$i]);
    }
}
print_r ($b);
Orotund answered 4/12, 2018 at 7:31 Comment(4)
Can you share some explanation about that code? For example, why don't you use a foreach loop?Shari
This is a sort form of doing and why I didn't use foreach because I'm much comfortable with for loop. That's it.Orotund
Nevertheless, you should share some explanation with the code. Posting such answers on SO is not a good style: people with the same problem as the OP could come along, and they should be able to understand how your code solves the problem such that they can learn from it and adopt it to their needsShari
in_array() in this example does not have its strict parameter set to true, so there may be some loose comparisons that trigger incorrect value eliminations in real projects. in_array() is probably one of PHP's slowest ways to search an array.Unnumbered
O
0

As an alternative of array_unique() you may use php Set class

$array = array(1, 2, 2, 3);
$array = (new \Ds\Set($array))->toArray() ; // Array is now (1, 2, 3)
Octameter answered 22/2, 2023 at 21:26 Comment(1)
I assume there is a greater cost to instantiate a class and call one of its methods versus merely calling a native function.Unnumbered

© 2022 - 2024 — McMap. All rights reserved.