php 如何检测PHP数组中的重复值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1170807/
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 01:19:19  来源:igfitidea点击:

How to detect duplicate values in PHP array?

phparrays

提问by mikey_w

I am working with a one dimensional array in PHP. I would like to detect the presence of duplicate values, then count the number of duplicate values and out put the results. For example, given the following array:

我正在使用 PHP 中的一维数组。我想检测重复值的存在,然后计算重复值的数量并输出结果。例如,给定以下数组:

$array = array('apple', 'orange', 'pear', 'banana', 'apple',
   'pear', 'kiwi', 'kiwi', 'kiwi');

I would like to print:

我想打印:

apple (2)
orange
pear (2)
banana
kiwi (3)

Any advice on how to approach this problem?

关于如何解决这个问题的任何建议?

Thanks.

谢谢。

Mike

麦克风

回答by Silfverstrom

You can use array_count_valuesfunction

您可以使用array_count_values函数

$array = array('apple', 'orange', 'pear', 'banana', 'apple',
'pear', 'kiwi', 'kiwi', 'kiwi');

print_r(array_count_values($array));

will output

会输出

Array
(
   [apple] => 2
   [orange] => 1
   [pear] => 2
   etc...
)

回答by Ritvick Paliwal

if(count(array_unique($array))<count($array))
{
    // Array has duplicates
}
else
{
    // Array does not have duplicates
}

回答by Anton Maryanov

function array_not_unique( $a = array() )
{
  return array_diff_key( $a , array_unique( $a ) );
}

回答by Anton Maryanov

To get rid use array_unique(). To detect if have any use count(array_unique())and compare to count($array).

摆脱使用array_unique(). 检测是否有任何用途count(array_unique())并与count($array).

回答by Simon Scarfe

You could try turning that array into a associative array with the fruits as keys and the number of occurrences as values. Bit long-winded, but it looks like:

您可以尝试将该数组转换为关联数组,以水果为键,以出现次数为值。有点啰嗦,但看起来像:

$array = array('apple', 'orange', 'pear', 'banana', 'apple',
   'pear', 'kiwi', 'kiwi', 'kiwi');
$new_array = array();
foreach ($array as $key => $value) {
    if(isset($new_array[$value]))
        $new_array[$value] += 1;
    else
        $new_array[$value] = 1;
}
foreach ($new_array as $fruit => $n) {
    echo $fruit;
    if($n > 1)
        echo "($n)";
    echo "<br />";
}

回答by Anton Gogolev

Stuff them into a map(pseudocode)

把它们塞进一个map(伪代码)

map[string -> int] $m
foreach($word in $array)
{
    if(!$m.contains($word))
        $m[$word] = 0;

    $m[$word] += 1;
}

回答by Anton Gogolev

Perhaps something like this (untested code but should give you an idea)?

也许是这样的(未经测试的代码,但应该给你一个想法)?

$new = array();

foreach ($array as $value)
{
    if (isset($new[$value]))
        $new[$value]++;
    else
        $new[$value] = 1;
}

Then you'll get a new array with the values as keys and their value is the number of times they existed in the original array.

然后你会得到一个以值作为键的新数组,它们的值是它们在原始数组中存在的次数。

回答by uday

$count = 0;
$output ='';
$ischeckedvalueArray = array();
for ($i=0; $i < sizeof($array); $i++) {
    $eachArrayValue = $array[$i];
    if(! in_array($eachArrayValue, $ischeckedvalueArray)) {
        for( $j=$i; $j < sizeof($array); $j++) {
            if ($array[$j] === $eachArrayValue) {
                $count++;
            }
        }
        $ischeckedvalueArray[] = $eachArrayValue;
        $output .= $eachArrayValue. " Repated ". $count."<br/>";
        $count = 0;
    }

}

echo $output;

回答by aswine

I didn't find the answer I was looking for, so I wrote this function. This will make an array that contains only the duplicates between the two arrays, but not print the number of times an element is duplicated, so it's not directly answering the question, but I'm hoping it'll help someone in my situation.

我没有找到我要找的答案,所以我写了这个函数。这将创建一个只包含两个数组之间重复项的数组,但不打印元素被复制的次数,因此它不会直接回答问题,但我希望它会帮助我的情况下的人。

function findDuplicates($array1,$array2)
{
    $combined = array_merge($array1,$array2);
    $counted = array_count_values($combined);
    $dupes = [];
    $keys = array_keys($counted);
    foreach ($keys as $key)
    {   
        if ($counted[$key] > 1)
        {$dupes[] = $key;}
    }
    sort($dupes);
    return $dupes;
}
$array1 = [1,2,3,4,5];
$array2 = [4,5,6,7,8];
$dupes = findDuplicates($array1,$array2);
print_r($dupes);

Outputs:

输出:

Array
(
    [0] => 4
    [1] => 5
)

回答by thelife

A simple method:

一个简单的方法:

$array = array_values(array_unique($array, SORT_REGULAR));