20

我有以下两个数组和查找array_diff的代码:

$obs_ws = array("you", "your", "may", "me", "my", "etc");
$all_ws = array("LOVE", "World", "Your", "my", "etc", "CoDe");

$final_ws = array_diff($all_ws, $obs_ws);

上面的代码给出了输出数组:

$final_ws = array("LOVE", "World", "Your", "CoDe");

但我希望它为:

$final_ws = array("LOVE", "World", "CoDe");

注意“你的”没有被删除,这可能是因为“Y”在第二个数组中是大写的。我也想排除“你的”,所以 PHP 中有任何不区分大小写的版本array_diff

我尝试了array_udiff,但我不知道如何在我的问题中使用它

谢谢

4

2 回答 2

66

尝试strcasecmp作为第三个参数传递给array_udiff函数:

<?php
$obs_ws = array("you", "your", "may", "me", "my", "etc");
$all_ws = array("LOVE", "World", "Your", "my", "etc", "CoDe");

$final_ws = array_udiff($all_ws, $obs_ws, 'strcasecmp');

print_r($final_ws);

输出:

Array
(
    [0] => LOVE
    [1] => World
    [5] => CoDe
)
于 2009-12-09T18:36:34.480 回答
3

你走在正确的轨道上。这是我的建议:

function array_casecmp($arr1,$arr2){
    return array_udiff($arr1,$arr2,'strcasecmp');
}


$obs_ws = array("you", "your", "may", "me", "my", "etc");
$all_ws = array("LOVE", "World", "Your", "my", "etc", "CoDe");
var_dump( array_casecmp($all_ws,$obs_ws) );
于 2009-12-09T18:52:19.623 回答