检查并返回重复数组php

时间:2021-11-27 07:40:35

I would like to check if my array has any duplicates and return the duplicated values in an array. I want this to be as efficient as possible.

我想检查我的数组是否有任何重复项并返回数组中的重复值。我希望这个尽可能高效。

Example :$array = array(1,2,2,4,5)
function returndup($array) should return 2 ;

if array is array(1,2,1,2,5);
it should return an array with 1,2

Also the initial array is always 5 positions long

初始数组总是长5个位置

6 个解决方案

#1


56  

this will be ~100 times faster than array_diff

这将比array_diff快约100倍

$dups = array();
foreach(array_count_values($arr) as $val => $c)
    if($c > 1) $dups[] = $val;

#2


12  

You can get the difference of the original array and a copy without duplicates using array_unique and array_diff_assoc:

您可以使用array_unique和array_diff_assoc来获取原始数组和没有重复项的副本的区别:

array_diff_assoc($arr, array_unique($arr))

#3


3  

You can do like this:

你可以这样做:

function showDups($array)
{
  $array_temp = array();

   foreach($array as $val)
   {
     if (!in_array($val, $array_temp))
     {
       $array_temp[] = $val;
     }
     else
     {
       echo 'duplicate = ' . $val . '<br />';
     }
   }
}


$array = array(1,2,2,4,5);
showDups($array);

Output:

输出:

duplicate = 2

#4


3  

function array_dup($ar){
   return array_unique(array_diff_assoc($ar,array_unique($ar)));
}

Should do the trick.

应该做的伎俩。

#5


1  

in addition to gumbo's answer:

除了gumbo的答案:

function returndup($arr)
{
  return array_diff_key($arr, array_unique($arr));
}

#6


1  

function returndup($array) 
{
    $results = array();
    $duplicates = array();
    foreach ($array as $item) {
        if (in_array($item, $results)) {
            $duplicates[] = $item;
        }

        $results[] = $item;
    }

    return $duplicates;
}

#1


56  

this will be ~100 times faster than array_diff

这将比array_diff快约100倍

$dups = array();
foreach(array_count_values($arr) as $val => $c)
    if($c > 1) $dups[] = $val;

#2


12  

You can get the difference of the original array and a copy without duplicates using array_unique and array_diff_assoc:

您可以使用array_unique和array_diff_assoc来获取原始数组和没有重复项的副本的区别:

array_diff_assoc($arr, array_unique($arr))

#3


3  

You can do like this:

你可以这样做:

function showDups($array)
{
  $array_temp = array();

   foreach($array as $val)
   {
     if (!in_array($val, $array_temp))
     {
       $array_temp[] = $val;
     }
     else
     {
       echo 'duplicate = ' . $val . '<br />';
     }
   }
}


$array = array(1,2,2,4,5);
showDups($array);

Output:

输出:

duplicate = 2

#4


3  

function array_dup($ar){
   return array_unique(array_diff_assoc($ar,array_unique($ar)));
}

Should do the trick.

应该做的伎俩。

#5


1  

in addition to gumbo's answer:

除了gumbo的答案:

function returndup($arr)
{
  return array_diff_key($arr, array_unique($arr));
}

#6


1  

function returndup($array) 
{
    $results = array();
    $duplicates = array();
    foreach ($array as $item) {
        if (in_array($item, $results)) {
            $duplicates[] = $item;
        }

        $results[] = $item;
    }

    return $duplicates;
}