您将需要使您的函数不区分大小写,以获得“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);
输出为:
Array
(
[3] => Hello
[4] => hello
)
由@AlixAxel编辑:
这个答案是非常误导的。它只适用于这种特定条件。这个反例:
$arr = array(1=>'1233',2=>'12334',3 =>'Hello' ,4=>'HELLO', 5=>'U');
Fails miserably.另外,这不是保持重复的方式:
array_diff($arr, array_unique($arr));
因为其中一个重复的值将在array_unique中,而是被array_diff切断。
由@RyanDay编辑:
所以看看@ Srikanth’s或@Bucabay的答案,它适用于所有案例(在Bucabay中寻找不区分大小写),而不仅仅是问题中指定的测试数据。