4

是否有任何类似于“array_intersect”的函数,但它处于不区分大小写且忽略波浪线的模式?

array_intersect PHP 函数将数组元素与 === 进行比较,所以我没有得到预期的结果。

例如,我想要这段代码:

$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);

输出绿色红色。在默认的 array_intersect 函数中只是红色(正常原因 ===)。

有什么解决办法吗?

先感谢您

4

2 回答 2

16
$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
于 2014-02-11T11:51:00.737 回答
7
<?php

function to_lower_and_without_tildes($str,$encoding="UTF-8") {
  $str = preg_replace('/&([^;])[^;]*;/',"$1",htmlentities(mb_strtolower($str,$encoding),null,$encoding));
  return $str;
}

function compare_function($a,$b) {
  return strcmp(to_lower_and_without_tildes($a), to_lower_and_without_tildes($b));
}

$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_uintersect($array1, $array2,"compare_function");
print_r($result);

输出:

Array
(
    [a] => gréen
    [0] => red
)
于 2014-02-11T12:06:43.270 回答