Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Sorting PHP array with locale setting?

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?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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',
];
*/
about 4 years ago · Santiago Trujillo Report

0

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);
about 4 years ago · Santiago Trujillo Report

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!