Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort an array of UTF-8 strings in PHP?

Tags:

need help with sorting words by utf-8. For example, we have 5 cities from Belgium.

$array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel'); sort($array); // Expected: Aubel, Borgloon, Éghezée, Lennik, Thuin               // Actual: Aubel, Borgloon, Lennik, Thuin, Éghezée 

City Éghezée should be third. Is it possible to use/set some kind of utf-8 or create my own character order?

like image 437
mesnicka Avatar asked Oct 28 '11 13:10

mesnicka


People also ask

How do you sort an array of strings?

To sort a String array in Java, you need to compare each element of the array to all the remaining elements, if the result is greater than 0, swap them.

How do you sort an array of objects in PHP?

Approach: The usort() function is an inbuilt function in PHP which is used to sort the array of elements conditionally with a given comparator function. The usort() function can also be used to sort an array of objects by object field.

What is K sort in PHP?

The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key. Tip: Use the asort() function to sort an associative array in ascending order, according to the value.


2 Answers

intl comes bundled with PHP from PHP 5.3 and it only supports UTF-8.

You can use a Collator in this case:

$array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel'); $collator = new Collator('en_US'); $collator->sort($array); print_r($array); 

Output:

Array (     [0] => Aubel     [1] => Borgloon     [2] => Éghezée     [3] => Lennik     [4] => Thuin ) 
like image 166
Thai Avatar answered Oct 08 '22 00:10

Thai


I think you can use strcoll:

setlocale(LC_COLLATE, 'nl_BE.utf8'); $array = array('Borgloon','Thuin','Lennik','Éghezée','Aubel'); usort($array, 'strcoll');  print_r($array); 

Result:

Array (     [0] => Aubel     [1] => Borgloon     [2] => Éghezée     [3] => Lennik     [4] => Thuin ) 

You need the nl_BE.utf8 locale on your system:

fy@Heisenberg:~$ locale -a | grep nl_BE.utf8 nl_BE.utf8 

If you are using debian you can use dpkg --reconfigure locales to add locales.

like image 39
Fy- Avatar answered Oct 08 '22 00:10

Fy-