Sort an array with special characters in PHP
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.