您需要使您的函数不区分大小写才能获得您正在寻找的“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 或@Bucabay 的答案,它们适用于所有情况(在 Bucabay 中查找不区分大小写),而不仅仅是问题中指定的测试数据。