How to remove duplicate values from an array in PHP

PhpArraysDuplicate Data

Php Problem Overview


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

Php Solutions


Solution 1 - Php

Use array_unique().

Example:

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

Solution 2 - Php

Use array_values(array_unique($array));

array_unique: for unique array array_values: for reindexing

Solution 3 - Php

//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 )

Solution 4 - Php

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.

Solution 5 - Php

$result = array();
foreach ($array as $key => $value){
  if(!in_array($value, $result))
    $result[$key]=$value;
}

Solution 6 - Php

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 */

Solution 7 - Php

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

Solution 8 - Php

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

Solution 9 - Php

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

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

Solution 10 - Php

Depending on the size of your array, I have found

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

can be faster than array_unique.

Solution 11 - Php

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

Solution 12 - Php

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

Solution 13 - Php

	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

Solution 14 - Php

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 )

Solution 15 - Php

If you concern in performance and have simple array, use:

array_keys(array_flip($array));

It's many times faster than array_unique.

Solution 16 - Php

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

Solution 17 - Php

$arrDuplicate = array ("","",1,3,"",5);
 foreach(array_unique($arrDuplicate) as $v){
  if($v != "" ){$arrRemoved = $v;  }}
print_r($arrRemoved);

Solution 18 - Php

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)
}

Solution 19 - Php

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

Solution 20 - Php

<?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;
}

Solution 21 - Php

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

Solution 22 - Php

function arrayUnique($myArray)
{
    $newArray = Array();
    if (is_array($myArray))
    {
        foreach($myArray as $key=>$val)
        {
            if (is_array($val))
            {
                $val2 = arrayUnique($val);
            }
            else
            {
                $val2 = $val;
                $newArray=array_unique($myArray);
                $newArray=deleteEmpty($newArray);
                break;
            }
            if (!empty($val2))
            {
                $newArray[$key] = $val2;
            }
        }
    }
    return ($newArray);
}

function deleteEmpty($myArray)
{
    $retArray= Array();
    foreach($myArray as $key=>$val)
    {
        if (($key<>"") && ($val<>""))
        {
            $retArray[$key] = $val;
        }
    }
    return $retArray;
}

Solution 23 - Php

An alternative for array_unique() function..

Using Brute force algorithm

//[1] This our array with duplicated items

$matches = ["jorge","melvin","chelsy","melvin","jorge","smith"];

//[2] Container for the new array without any duplicated items

$arr = [];

//[3] get the length of the duplicated array and set it to the var len to be use for for loop

$len = count($matches);

//[4] If matches array key($i) current loop Iteration is not available in //[4] the array $arr then push the current iteration key value of the matches[$i] //[4] to the array arr.

for($i=0;$i<$len;$i++){

if(array_search($matches[$i], $arr) === false){

    array_push($arr,$matches[$i]);
}
}

//print the array $arr.
print_r($arr);

//Result: Array
(
[0] => jorge
[1] => melvin
[2] => chelsy
[3] => smith
)

Solution 24 - Php

<?php
$a=array("1"=>"302","2"=>"302","3"=>"276","4"=>"301","5"=>"302");
print_r(array_values(array_unique($a)));
?>//`output -> Array ( [0] => 302 [1] => 276 [2] => 301 )`

Solution 25 - Php

$array = array("a" => "moon", "star", "b" => "moon", "star", "sky");
 
// Deleting the duplicate items
$result = array_unique($array);
print_r($result);

ref : Demo

Solution 26 - Php

I have done this without using any function.

$arr = array("1", "2", "3", "4", "5", "4", "2", "1");

$len = count($arr);
for ($i = 0; $i < $len; $i++) {
  $temp = $arr[$i];
  $j = $i;
  for ($k = 0; $k < $len; $k++) {
    if ($k != $j) {
      if ($temp == $arr[$k]) {
        echo $temp."<br>";
        $arr[$k]=" ";
      }
    }
  }
}

for ($i = 0; $i < $len; $i++) {
  echo $arr[$i] . " <br><br>";
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIanView Question on Stackoverflow
Solution 1 - PhpPaige RutenView Answer on Stackoverflow
Solution 2 - Phpnimey sara thomasView Answer on Stackoverflow
Solution 3 - PhpchimView Answer on Stackoverflow
Solution 4 - PhpiniravpatelView Answer on Stackoverflow
Solution 5 - PhpAmr BeragView Answer on Stackoverflow
Solution 6 - PhpAgelessEssenceView Answer on Stackoverflow
Solution 7 - Phpharsh kumarView Answer on Stackoverflow
Solution 8 - Phppawan kumarView Answer on Stackoverflow
Solution 9 - PhpDebView Answer on Stackoverflow
Solution 10 - PhpBollisView Answer on Stackoverflow
Solution 11 - PhpShahrukh AnwarView Answer on Stackoverflow
Solution 12 - PhpDries BView Answer on Stackoverflow
Solution 13 - PhpAlpesh NavadiyaView Answer on Stackoverflow
Solution 14 - PhpShivivanandView Answer on Stackoverflow
Solution 15 - Phpmichal.jakubeczyView Answer on Stackoverflow
Solution 16 - PhpRishi ChowdhuryView Answer on Stackoverflow
Solution 17 - Phpuser1045247View Answer on Stackoverflow
Solution 18 - PhpRohit SutharView Answer on Stackoverflow
Solution 19 - PhpMirza ObaidView Answer on Stackoverflow
Solution 20 - PhpsumityadavbadliView Answer on Stackoverflow
Solution 21 - PhpAladin BanwalView Answer on Stackoverflow
Solution 22 - PhpVineesh KalarickalView Answer on Stackoverflow
Solution 23 - PhpRichard RamosView Answer on Stackoverflow
Solution 24 - PhpMajid shiriView Answer on Stackoverflow
Solution 25 - Phpnageen nayakView Answer on Stackoverflow
Solution 26 - PhpAshishdmc4View Answer on Stackoverflow