Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda