1

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:

setlocale(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.

hakre
  • 193,403
  • 52
  • 435
  • 836
David Savage
  • 1,562
  • 2
  • 18
  • 35
  • 3
    Are you sure you are getting the language code right? – Pekka Mar 31 '11 at 20:21
  • Works here, so indeed, most likely locale issue. – Wrikken Mar 31 '11 at 20:25
  • I'm not certain, I based 'fre' off of this: "Category/locale names can be found in » RFC 1766 and » ISO 639. Different systems have different naming schemes for locales." from http://php.net/manual/en/function.setlocale.php and I used the second ISO 639 code on the linked page (showed fra/fre) – David Savage Mar 31 '11 at 20:26
  • 2
    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. – Mark Baker Mar 31 '11 at 20:35
  • It's a Linux install, guess I'll see what I can figure from that.. – David Savage Mar 31 '11 at 20:36
  • You should make that a suggested answer Mark, that was the issue. fr_FR makes it work! – David Savage Mar 31 '11 at 20:49
  • @David - duly done, for the benefit of others that might fall fould of the same problem; though Pekka really deserves the credit – Mark Baker Mar 31 '11 at 21:22

2 Answers2

2

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.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • See http://msdn.microsoft.com/en-us/library/39cwe7zf%28vs.71%29.aspx for Winows locale codes. They are different and not every locale is supported. – Halil Özgür Mar 17 '12 at 14:12
1

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