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

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

    There are great answers here, but this is a dead simple solution for most situations.

    function globalsort($array, $in = 'UTF-8', $out = 'ASCII//TRANSLIT//IGNORE')
    {
        return usort($array, function ($a, $b) use ($in, $out) {
            $a = @iconv($in, $out, $a);
            $b = @iconv($in, $out, $b);
            return strnatcasecmp($a, $b);
        });
    }
    

    And use it like so:

    globalsort($array);
    

提交回复
热议问题