PHP:不区分大小写的“array_diff”

发布于 2024-08-14 06:58:28 字数 663 浏览 4 评论 0原文

我有以下两个数组和查找 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");

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

我尝试了 array_udiff 但我没有得到到底如何在我的问题中使用它

谢谢

I have following two arrays and the code to find 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);

Above code giving output array as:

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

But I want it as:

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

Note "Your" is not removed, it may be due to "Y" is in caps in second array. I want to exclude "Your" also, so is there any case-insensitive version of array_diff in PHP.

I tried array_udiff but I am not getting exactly how to use this in my problem

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-08-21 06:58:28

尝试将 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
)

Try to pass strcasecmp as third parameter to array_udiff function:

<?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);

Output:

Array
(
    [0] => LOVE
    [1] => World
    [5] => CoDe
)
孤芳又自赏 2024-08-21 06:58:28

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

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) );

You were on the right track. This is my suggestion:

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) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文