3

Is it possible to sort a PHP array with a locale setting?

This is the setup:

I am making an interactive sorted list in PHP. By user input, one of a number of categories (columns) can be made to direct the sorting (name, residence, etc). This I worked out by using array_multisort() function.

Next hurdle. The list is in Swedish and the user will expect Swedish alphabetical order: abcdefghijklmnopqrstuvxyzåäö. Right now the interpreter sorts åäö as non-alphabetical and places them before "a". How to remedy?

I found some scattered info on a setlocale(LC_COLLATE, "sv_SV") function, but the reviews were not rave and I did not manage to comprehend how it could be used with array_multisort(). Can it? and if so, how? Is there another way within php?

Thing is, there must be some way – Swedish websites abound where Swedish sort order is applied. Can it be done with php?

3 Answers 3

4

Use collator_sort or collator_asort.

// sorting properly accented "Č" in Czech language (should come after "C" and before "D")

// 1) Simple sort:
$array = ['bca', 'čaz', 'cba', 'abc', 'daz'];
$collator = collator_create('cs-CZ');
collator_sort($collator, $array);
// result:
// ['abc', 'bca', 'cba', 'čaz', 'daz']

// 2) Maintain index assotiations:
$array = [
    'x' => 'Česko',
    'y' => 'Dänmark',
    'z' => 'Brunei',
    'w' => 'Cyprus'
];
$coll = collator_create('sk'); // set Slovak locale (or sk-SK)
collator_asort($coll, $array);

/* result:
$array = [
    'z' => 'Brunei',
    'w' => 'Cyprus',
    'x' => 'Česko',
    'y' => 'Dänmark',
];
*/
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thank you very much! This works like a charm.
1

Use SORT_LOCALE_STRING as the third parameter of array_multisort() function. PHP ducuments say:

SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()

example:

$result=array_multisort( $input_array, SORT_ASC, SORT_LOCALE_STRING);

Comments

0

Sorry. This was not the problem I thought it was. Text written in my code gets sorted sort of correctly (only that the ä gets incorrectly sorted before å, but that seems to a bug in the specs(?)).

Anyway, the problem is obviously with character encoding. It is when text is fetched from a Contact Form 7 database (Wordpress plugin) that the problem arises. Presumably it has another encoding and needs converting.

Thanks anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.