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

cod*_*rex 22 php arrays

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

array(
    1 => '1233',
    2 => '12334',
    3 => 'Hello',
    4 => 'hello',
    5 => 'U'
);
Run Code Online (Sandbox Code Playgroud)

我想返回一个只有重复值的数组:"hello".

期望的输出数组:

array(
    1 => 'Hello',
    2 => 'hello'
);
Run Code Online (Sandbox Code Playgroud)

Shi*_*idi 23

<?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] = 'abc@xyz.com';
$raw_array[2] = 'def@xyz.com';
$raw_array[3] = 'ghi@xyz.com';
$raw_array[4] = 'abc@xyz.com'; // Duplicate

$common_stuff = array_not_unique($raw_array);
var_dump($common_stuff);
Run Code Online (Sandbox Code Playgroud)


Alu*_*ard 17

function get_duplicates ($array) {
    return array_unique( array_diff_assoc( $array, array_unique( $array ) ) );
}
Run Code Online (Sandbox Code Playgroud)


rya*_*day 16

您将需要使您的函数大小写不敏感以获得您正在寻找的"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);
Run Code Online (Sandbox Code Playgroud)

输出是:

Array
(
[3] => Hello
[4] => hello
)
Run Code Online (Sandbox Code Playgroud)

由@AlixAxel编辑:

这个答案非常误导.它只适用于这种特定条件.这个反例:

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

悲惨地失败.此外,这不是保持重复的方法:

array_diff($arr, array_unique($arr));
Run Code Online (Sandbox Code Playgroud)

由于其中一个重复值将进入array_unique,然后被切断array_diff.

由@RyanDay编辑:

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

  • -1,这是完全失败:http://codepad.org/7NQ9lQLU.只有纯粹的机会才有效. (6认同)
  • 浪费了我15分钟的嘘声@AkshatSinghal评论 (2认同)

buc*_*bay 6

尝试:

$arr2 = array_diff_key($arr, array_unique($arr));
Run Code Online (Sandbox Code Playgroud)

不区分大小写:

array_diff_key($arr, array_unique(array_map('strtolower', $arr)));
Run Code Online (Sandbox Code Playgroud)

  • -1,重复值之一将被删除,不返回所有重复值。 (3认同)

buc*_*bay 6

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;
}
Run Code Online (Sandbox Code Playgroud)

Srikanth(约翰)添加了什么,但不区分大小写.


Ali*_*xel 6

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

array_intersect($arr, array_unique(array_diff_key($arr, array_unique($arr))));
Run Code Online (Sandbox Code Playgroud)

并且不区分大小写的解决方案:

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

array_intersect_key($arr, $iArr);
Run Code Online (Sandbox Code Playgroud)

但是@Srikanth的答案更有效率(实际上,除了这个之外,它是唯一正常工作的答案).