Jak usunąć zduplikowane wartości z tablicy w PHP

Jak mogę usunąć zduplikowane wartości z tablicy w PHP?

Author: HamZa, 2008-11-21

20 answers

Użyj array_unique () .

Przykład:

$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
 202
Author: Jeremy Ruten,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-11-21 02:39:33
//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 )
 21
Author: chim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-06 11:30:13

Użyj array_values(array_unique($array));

array_unique: dla unikalnej tablicy array_values: dla reniferów

 11
Author: nimey sara thomas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-15 13:50:08

Sometimes array_unique() is not the way, jeśli chcesz uzyskać unikalne i zduplikowane elementy ...

$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

Wyniki na

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

array ( 0 => '', 1 => '', 2 => '', 3 => 'A1', ) /* $duplicated */
 5
Author: AgelessEssence,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-13 22:57:48

Jedyne co mi się udało to:

$array = array_unique($array, SORT_REGULAR);
 4
Author: niravpatel9898,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-12 18:57:41
$result = array();
foreach ($array as $key => $value){
  if(!in_array($value, $result))
    $result[$key]=$value;
}
 3
Author: Amr Berag,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-12 21:05:16

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

To zajmie się asocjacją kluczy i serializacją kluczy dla wynikowej nowej tablicy: -)

 2
Author: Deb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-18 12:48:22

Możemy stworzyć taki typ tablicy, aby ta ostatnia wartość została zaktualizowana do wartości kolumny lub klucza i z tablicy otrzymamy unikalną wartość...

$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);
 2
Author: harsh kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-29 12:36:44

To świetny sposób. Może warto się upewnić, że jego wyjście jest z powrotem tablicą. Teraz pokazujesz tylko ostatnią unikalną wartość.

Spróbuj tego:

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

foreach (array_unique($arrDuplicate) as $v){
  if($v != "") { $arrRemoved[] = $v; }
}
print_r ($arrRemoved);
 1
Author: Dries B,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-05 08:56:24

W zależności od wielkości tablicy, znalazłem

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

Może być szybszy niż array_unique.

 1
Author: Bollis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-31 03:59:09
    if (@!in_array($classified->category,$arr)){        
                                    $arr[] = $classified->category;
                                 ?>

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

Po raz pierwszy sprawdź wartość w tablicy i znajdź tę samą wartość ignoruj ją

 1
Author: Alpesh Navadiya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-15 05:51:15

Usuń zduplikowane wartości z tablicy asocjacyjnej w 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 )

 1
Author: Shivivanand,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-12 20:39:32
$arrDuplicate = array ("","",1,3,"",5);
 foreach(array_unique($arrDuplicate) as $v){
  if($v != "" ){$arrRemoved = $v;  }}
print_r($arrRemoved);
 0
Author: user1045247,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-14 09:15:34
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;
}
 0
Author: Vineesh Kalarickal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-06 23:30:52

Spróbuj tego krótkiego i słodkiego kodu -

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

Wyjście -

array(6) {
  [0]=>
  int(1)
  [1]=>
  int(4)
  [2]=>
  int(2)
  [3]=>
  int(7)
  [4]=>
  int(9)
  [5]=>
  int(5)
}
 0
Author: Rohit Suthar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-01 09:47:00

Można to zrobić za pomocą funkcji wykonałem trzy duplikaty funkcji zwracające wartości, które są duplikowane w tablicy .druga funkcja single zwraca tylko te wartości, które są pojedyncze, co oznacza, że nie są powtarzane w tablicy, a trzecia i pełna funkcja zwracają wszystkie wartości, ale nie są duplikowane jeśli jakakolwiek wartość jest duplikowana, konwertuje ją na pojedynczą

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;

}

}

 0
Author: Mirza Obaid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-27 06:45:09
<?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;
}
 0
Author: sumityadavbadli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-24 08:35:07

Istnieje wiele sposobów, aby to zrobić, które są następujące

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

//second method
$array = array_unique($arr, SORT_REGULAR);
 0
Author: Shahrukh Anwar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-05 08:33:32

Jeśli chodzi o wydajność i masz prostą tablicę, użyj:

array_keys(array_flip($array));

Jest wiele razy szybszy niż array_unique.

 0
Author: jabko87,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-10-01 12:59:42

Zrobiłem to bez użycia żadnej funkcji.

$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>";
}
 -1
Author: Ashishdmc4,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-18 12:46:31