How can I get case-sensitive return from array_intersect()

烈酒焚心 提交于 2019-11-30 17:20:13

问题


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

My result is,

Array
(
    [a] => green
    [0] => red
)

But my expected result is I want get it from array1 like:

Array
(
    [a] => Green
    [0] => Red
)

回答1:


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

output:

Array ( [a] => Green [0] => Red )


来源:https://stackoverflow.com/questions/30138189/how-can-i-get-case-sensitive-return-from-array-intersect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!