我如何从array_intersect()获得区分大小写的返回 [英] How can I get case-sensitive return from array_intersect()
本文介绍了我如何从array_intersect()获得区分大小写的返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数组,我需要比较它们并从array1返回匹配的值.请在下面引用我的代码,
I have two arrays and I need to compare that and return matched value from array1. Please refer my code below,
$array1 = array("a" => "Green", "Red", "Blue");
$array2 = array("b" => "grEEn", "yellow", "red");
$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
print_r($result);
我的结果是,
Array
(
[a] => green
[0] => red
)
但是我的预期结果是我想从array1中获得它,如:
But my expected result is I want get it from array1 like:
Array
(
[a] => Green
[0] => Red
)
推荐答案
这是因为您将所有值都转换为小写.只需更改为 array_uintersect()
并使用
This is because you put all values to lowercase. Just change to array_uintersect()
and use strcasecmp()
as callback function to compare them case-insensitive, like this:
$result = array_uintersect($array1, $array2, "strcasecmp");
输出:
Array ( [a] => Green [0] => Red )
这篇关于我如何从array_intersect()获得区分大小写的返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文