开发者

PHP ksort seems unaffected by setlocale

I've been given an array that needs to be sorted by its key (associative array), and it may have some accented characters in it (à, è, ì, etc.). If it helps any, I do know the language that will be used, so I should be able to set a locale in theory. So, my original theory was to do it like so:

setlocal开发者_开发问答e(LC_COLLATE, 'fre');
ksort($array, SORT_LOCALE_STRING);

This doesn't appear to change the way ksort works at all - Accented characters are always last alphabetically when i'd think they should at least come after regular characters. For instance, 'èvery' would come after 'every', but not after 'fair'. So, then I started to look into collator but couldn't find a way to sort by the array keys. If anyone has any ideas it would be greatly appreciated.


Is this on a Windows or a Linux (or Mac) server? I believe the locale codes are slightly different on Windows... e.g. 'nl_NL' on Linux and 'nld_nld' on Windows.


On Linux, you can try the command "locale" to know your locale.

$ locale

LANG=fr_CA.UTF-8
LANGUAGE=
LC_CTYPE="fr_CA.UTF-8"
LC_NUMERIC=fr_CA.UTF-8
LC_TIME=fr_CA.UTF-8
LC_COLLATE="fr_CA.UTF-8"
LC_MONETARY=fr_CA.UTF-8
LC_MESSAGES="fr_CA.UTF-8"
LC_PAPER=fr_CA.UTF-8
LC_NAME=fr_CA.UTF-8
LC_ADDRESS=fr_CA.UTF-8
LC_TELEPHONE=fr_CA.UTF-8
LC_MEASUREMENT=fr_CA.UTF-8
LC_IDENTIFICATION=fr_CA.UTF-8
LC_ALL=

Then,

  setlocale(LC_ALL, 'fr_CA.UTF-8');
  ksort($my, SORT_LOCALE_STRING);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜