我一直在与同样的问题作斗争。您可以将setlocale()与asort ($array ,SORT_LOCALE_STRING) 一起使用。这将 ÅÄÖ 字符放在最后。不幸的是,它们的顺序错误。PHP 将它们排序为ä、å、ö。在北欧语言中(至少在瑞典字母表中),它们的排序如下:å、ä、ö。经过大量谷歌搜索后,我无法使用 PHP 原生功能解决此问题,因此我制作了自己的“修补”排序,我认为我可以分享。
我绝不是编写针对计算速度优化的代码的专家,所以我确信可以对这段代码进行一些改进。欢迎大家指出来!
此函数根据整个字符串对数组进行排序,而不仅仅是第一个字符。但我相信这是最有用的方法,如果你想在第一个字符之后忽略所有字符,我相信你可以修改它。此外,修改它以应用于其他字符和其他排序顺序也很容易。享受
function sort_nordic(&$array) {
uasort($array, 'nordic_cmp');
}
function nordic_cmp($a, $b) {
// If å, ä, and ö is missing from first string, just use PHP's native function
if (preg_match('/([å]|[ä]|[ö]|[Å]|[Ä]|[Ö])/', $a) == 0) {
return strcoll($a, $b);
}
// If å, ä, and ö is missing from second string, also use PHP's native function
if (preg_match('/([å]|[ä]|[ö]|[Å]|[Ä]|[Ö])/', $b) == 0) {
return strcoll($a, $b);
}
// Arriving here both the strings contains some characters we have too look out for
// First, create arrays from the strings so we can easily loop over the characters
// Comparison is made in lowercase
$a_arr = preg_split('//u', mb_strtolower($a), -1, PREG_SPLIT_NO_EMPTY);
$b_arr = preg_split('//u', mb_strtolower($b), -1, PREG_SPLIT_NO_EMPTY);
// Get the length of the shortest string
$end = min(mb_strlen($a), mb_strlen($b));
// Loop over the characters and compare them in pairs
for ($i = 0; $i < $end; $i++) {
// Check if character in the first string is one that we have to correct for
if (mb_stripos("åäö", $a_arr[$i]) !== false) {
// Computes the corrected return value. The first character "-" is just a
// nonsene placeholder to return 1 for "ä", 2 for "å" and 3 for "ö"
$r = mb_stripos("-åäö", $a_arr[$i]) - mb_stripos("-åäö", $b_arr[$i]);
if ($r != 0) {
return $r;
}
} else {
// If the character is not a character that we have to correct for
// the PHP native works fine
$r = strcoll($a_arr[$i], $b_arr[$i]);
if ($r != 0) {
return $r;
}
}
}
// Fallback: If so far there has been no call to return() then the
// strings are idenical up until the length of the shorter string.
// Then let the lengths of the strings determine the order
return mb_strlen($a) - mb_strlen($b);
}