简体   繁体   中英

PHP's strcasecmp behaving strangely

The PHP code below

$input1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$input2 = array("a" => "GREEN", "B" => "brown", "yellow", "RED");

$result = array_intersect_uassoc($input1, $input2, "strcasecmp");
print_r($result);

Is returning value

Array ( [b] => brown )

Am wondering since strcasecmp is case insensitive, why is "green" and "Green" not considered as matching.

Your misunderstanding isn't with strcasecmp, it's with array_intersect_uassoc. The last argument is not used to compare the values, it's used to compare the keys.

Therefore, the case insensitive check is not on the value, it's on the key itself which is why the "b" and "B" indexes intersect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM