php 检查并返回重复数组php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3450022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:48:56  来源:igfitidea点击:

Check and return duplicates array php

phparraysduplicates

提问by NVG

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 个位置

回答by user187291

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;

回答by Gumbo

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

您可以使用array_uniqueand获得原始数组和没有重复的副​​本的差异 array_diff_assoc

array_diff_assoc($arr, array_unique($arr))

回答by JAL

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

Should do the trick.

应该做的伎俩。

回答by JAL

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

回答by faebser

in addition to gumbo's answer:

除了秋葵汤的回答:

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

回答by Ronn0

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

        $results[] = $item;
    }

    return $duplicates;
}

回答by Stefan Gabos

I did some tests and indeed @user187291's variant is the fastest. But, it turns out that @Gumbo's and @faebser's alternative are almost as fast, @faebser's being just slightly faster than @Gumbo's and sometimes even fastest of all.

我做了一些测试,确实@user187291 的变体是最快的。但是,事实证明@Gumbo 和@faebser 的替代方案几乎一样快,@faebser 仅比@Gumbo 稍快,有时甚至是最快的。

Here's the code I used

这是我使用的代码

$array = array(1, "hello", 1, "world", "hello");
$times = 1000000;

$start = microtime(true);
for ($i = 0; $i < $times; $i++) {
    $dups = array();
    foreach(array_count_values($array) as $val => $c)
        if( $c > 1) $dups[] = $val;
}
$end = microtime(true);

echo 'variant 1 (user187291): ' . ($end - $start);
echo '<br><br><br>';

$start = microtime(true);
for ($i = 0; $i < $times; $i++)
    $dups = array_unique(array_diff_assoc($array, array_unique($array)));
$end = microtime(true);

echo 'variant 2 (JAL): ' . ($end - $start);
echo '<br><br><br>';

$start = microtime(true);
for ($i = 0; $i < $times; $i++)
    $dups = array_diff_assoc($array, array_unique($array));
$end = microtime(true);

echo 'variant 3 (Gumbo): ' . ($end - $start);
echo '<br><br><br>';

$start = microtime(true);
for ($i = 0; $i < $times; $i++)
    $dups = array_diff_key($array, array_unique($array));
$end = microtime(true);

echo 'variant 4 (faebser): ' . ($end - $start);
echo '<br><br><br>';

回答by Nemo

I have found another way to return duplicates in an array

我找到了另一种返回数组中重复项的方法

function printRepeating($arr, $size) 
{ 
    $i; 
    $j; 
    for($i = 0; $i < $size; $i++) 
        for($j = $i + 1; $j < $size; $j++) 
            if($arr[$i] == $arr[$j]) 
                echo $arr[$i], " "; 
}  



printRepeating($array, sizeof($array,0);

回答by raviranjan prasad

$duplicate_array = array();

$duplicate_array = array();

  for($i=0;$i<count($array);$i++){

    for($j=0;$j<count($array);$j++){

      if($i != $j && $array[$i] == $array[$j]){

        if(!in_array($array[$j], $duplicate_array)){

          $duplicate_array[] = $array[$j];

        }

      }

    }    

  }