Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda