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

前端 未结 7 2201
眼角桃花
眼角桃花 2020-11-27 20:24

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

$array = array(\'Borgloon\',\'Thuin\',\'Lennik\',\'Éghezée\',\'Aubel\');
s         


        
7条回答
  •  没有蜡笔的小新
    2020-11-27 20:36

    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.

提交回复
热议问题