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'
);
6 个解决方案
#1
16
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中查找不区分大小写),而不仅仅是问题中指定的测试数据。
#2
20
<?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);
#3
10
function get_duplicates ($array) {
return array_unique( array_diff_assoc( $array, array_unique( $array ) ) );
}
#4
5
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(约翰)添加了什么,但不区分大小写。
#5
4
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 @Srikanth answer is more efficient (actually, it's the only one that works correctly besides this one).
但是@Srikanth的答案更有效率(实际上,除了这个之外,它是唯一正常工作的答案)。
#6
2
Try:
尝试:
$arr2 = array_diff_key($arr, array_unique($arr));
case insensitive:
不区分大小写:
array_diff_key($arr, array_unique(array_map('strtolower', $arr)));
#1
16
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中查找不区分大小写),而不仅仅是问题中指定的测试数据。
#2
20
<?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);
#3
10
function get_duplicates ($array) {
return array_unique( array_diff_assoc( $array, array_unique( $array ) ) );
}
#4
5
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(约翰)添加了什么,但不区分大小写。
#5
4
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 @Srikanth answer is more efficient (actually, it's the only one that works correctly besides this one).
但是@Srikanth的答案更有效率(实际上,除了这个之外,它是唯一正常工作的答案)。
#6
2
Try:
尝试:
$arr2 = array_diff_key($arr, array_unique($arr));
case insensitive:
不区分大小写:
array_diff_key($arr, array_unique(array_map('strtolower', $arr)));