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

前端 未结 7 2198
眼角桃花
眼角桃花 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:59

    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
    )
    

提交回复
热议问题