PHP array_intersect case-insensitive and ignoring tildes

烂漫一生 提交于 2020-01-01 12:21:47

问题


Is there any function similar to "array_intersect" but it is in mode case-insensitive and ignoring tildes?

The array_intersect PHP function compares array elements with === so I do not get the expected result.

For example, I want this code :

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

Outputs gréen and red. In default array_intersect function just red is proposed (normal cause ===).

Any solution ?

Thank you in advance


回答1:


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

output:

Array
(
    [a] => gréen
    [0] => red
)



回答2:


$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));


来源:https://stackoverflow.com/questions/21700956/php-array-intersect-case-insensitive-and-ignoring-tildes

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