php php只返回数组中重复的条目

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

php return only duplicated entries from an array

phparrays

提问by coderex

I want to retrieve all duplicated entries from a array. Is this possible in PHP?

我想从数组中检索所有重复的条目。这在 PHP 中可能吗?

array(
    1 => '1233',
    2 => '12334',
    3 => 'Hello',
    4 => 'hello',
    5 => 'U'
);

I want to return an array with just the duplicate value: “hello”.

我想返回一个只有重复值的数组:“hello”。

Desired output array:

所需的输出数组:

array(
    1 => 'Hello',
    2 => 'hello'
);

采纳答案by ryanday

You will need to make your function case insensitive to get the "Hello" => "hello" result you are looking for, try this method:

您需要使您的函数不区分大小写以获得您正在寻找的“Hello”=>“hello”结果,试试这个方法:

$arr = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'hello', 5=>'U');

// Convert every value to uppercase, and remove duplicate values
$withoutDuplicates = array_unique(array_map("strtoupper", $arr));

// The difference in the original array, and the $withoutDuplicates array
// will be the duplicate values
$duplicates = array_diff($arr, $withoutDuplicates);
print_r($duplicates);

Output is:

输出是:

Array
(
[3] => Hello
[4] => hello
)


Edit by @AlixAxel:

@AlixAxel 编辑:

This answer is very misleading. It only works in this specific condition. This counter-example:

这个答案非常具有误导性。它仅在此特定条件下有效。这个反例:

$arr = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'HELLO', 5=>'U');

Fails miserably. Also, this is not the way to keep duplicates:

惨败。此外,这不是保持重复的方法:

array_diff($arr, array_unique($arr));

Since one of the duplicated values will be in array_unique, and then chopped off by array_diff.

由于重复值之一将在 中array_unique,然后被 切掉array_diff

Edit by @RyanDay:

@RyanDay 编辑:

So look at @Srikanth's or @Bucabay's answer, which work for all cases (look for case insensitive in Bucabay's), not just the test data specified in the question.

所以看看@Srikanth 或@Bucabay 的答案,它们适用于所有情况(在 Bucabay 中寻找不区分大小写的),而不仅仅是问题中指定的测试数据。

回答by Shiva Srikanth Thummidi

<?php
function array_not_unique($raw_array) {
    $dupes = array();
    natcasesort($raw_array);
    reset($raw_array);

    $old_key   = NULL;
    $old_value = NULL;
    foreach ($raw_array as $key => $value) {
        if ($value === NULL) { continue; }
        if (strcasecmp($old_value, $value) === 0) {
            $dupes[$old_key] = $old_value;
            $dupes[$key]     = $value;
        }
        $old_value = $value;
        $old_key   = $key;
    }
    return $dupes;
}

$raw_array    = array();
$raw_array[1] = '[email protected]';
$raw_array[2] = '[email protected]';
$raw_array[3] = '[email protected]';
$raw_array[4] = '[email protected]'; // Duplicate

$common_stuff = array_not_unique($raw_array);
var_dump($common_stuff);

回答by Alucard

function get_duplicates ($array) {
    return array_unique( array_diff_assoc( $array, array_unique( $array ) ) );
}

回答by bucabay

function array_not_unique($raw_array) {
    $dupes = array();
    natcasesort($raw_array);
    reset($raw_array);

    $old_key   = NULL;
    $old_value = NULL;
    foreach ($raw_array as $key => $value) {
        if ($value === NULL) { continue; }
        if (strcasecmp($old_value, $value) === 0) {
            $dupes[$old_key] = $old_value;
            $dupes[$key]     = $value;
        }
        $old_value = $value;
        $old_key   = $key;
    } return $dupes;
}

What Srikanth(john) added but with the case insensitive comparison.

什么SRIKANTH(约翰)添加,但与区分大小写的比较。

回答by Alix Axel

This is the correct way to do it (case-sensitive):

这是正确的方法(区分大小写):

array_intersect($arr, array_unique(array_diff_key($arr, array_unique($arr))));

And a case-insensitive solution:

以及不区分大小写的解决方案:

$iArr = array_map('strtolower', $arr);
$iArr = array_intersect($iArr, array_unique(array_diff_key($iArr, array_unique($iArr))));

array_intersect_key($arr, $iArr);

But @Srikanthanswer is more efficient (actually, it's the only one that works correctlybesides this one).

但是@Srikanth 的答案更有效(实际上,它是除此之外唯一可以正常工作的答案)。

回答by bucabay

Try:

尝试:

$arr2 = array_diff_key($arr, array_unique($arr));


case insensitive:

不区分大小写:

array_diff_key($arr, array_unique(array_map('strtolower', $arr)));