PHP에서 특수 문자로 배열을 정렬합니다
문제
스페인어로 언어의 이름을 보유하는 배열이 있습니다.
$lang["ko"] = "coreano"; //korean
$lang["ar"] = "árabe"; //arabic
$lang["es"] = "español"; //spanish
$lang["fr"] = "francés"; //french
.
배열을 주문하고 인덱스 연결을 유지해야하므로
> 강력한> SORT_LOCALE_STRING
setlocale(LC_ALL,'es_ES.UTF-8'); //this is at the beginning (config file)
asort($lang,SORT_LOCALE_STRING);
print_r($lang);
.
예상 출력은이 순서로 표시됩니다.
- 배열 ([ar]=> árabe [ko]=> coano [es]=> español [fr]=> francés)
그러나 이것은 내가받는 것입니다 :
- 배열 ([ko]=> Coreano [es]=> español [fr]=> Francés [ar]=> árabe)
뭔가를 놓치고 있습니까?귀하의 의견을 보내 주셔서 감사합니다!(내 서버는 PHP 버전 5.2.13을 사용하고 있음)
- 배열 ([ko]=> Coreano [es]=> español [fr]=> Francés [ar]=> árabe)
해결책
Try sorting by translitterated names:
function compareASCII($a, $b) {
$at = iconv('UTF-8', 'ASCII//TRANSLIT', $a);
$bt = iconv('UTF-8', 'ASCII//TRANSLIT', $b);
return strcmp($at, $bt);
}
uasort($lang, 'compareASCII');
print_r($lang);
다른 팁
You defined your locale incorrectly in setlocale()
.
Change:
setlocale(LC_ALL,'es_ES.UTF-8');
To:
setlocale(LC_ALL,'es_ES');
Output:
Array ( [ar] => árabe [ko] => coreano [es] => español [fr] => francés )
The documentation for setlocale
mentions that
Different systems have different naming schemes for locales.
It's possible that your system does not recognize the locale as es_ES
. If you are on Windows, try esp_ESP
instead.
Try this
setlocale(LC_COLLATE, 'nl_BE.utf8');
$array = array('coreano','árabe','español','francés');
usort($array, 'strcoll');
print_r($array);
제휴하지 않습니다 StackOverflow