在多维数组的 php 中排序时忽略重音字符

ignore accented characters while sorting in php in multidimensional array

我有如下所示的多维数组,我想在其中根据 [name] 字段进行排序。此外,带重音符号的字母应该像没有重音符号一样排序。

Array
(
    [chicago] => Array
        (
            [community_name] => Chicago, IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => HELLO WORLD.
                                )
                        )

                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Hello
                                )

                        )

                    [2] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Administration.
                                )
                        )
                )

        )

    [chicago-and-surrounding-areas] => Array
        (
            [community_name] => Chicago (and surrounding areas), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Covit Corp. 
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Câble-Axion Digital Corp. 
                                )
                        )   
                )

        )

    [cambridge-chicago] => Array
        (
            [community_name] => Cambridge (Chicago), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Avocados.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Aṕple.
                                )
                        )   
                )

        )

)

这就是我想要实现的:

Array
(
    [chicago] => Array
        (
            [community_name] => Chicago, IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Administration.
                                )
                        )

                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => HELLO WORLD. 
                                )

                        )

                    [2] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Hello
                                )
                        )
                )

        )

    [chicago-and-surrounding-areas] => Array
        (
            [community_name] => Chicago (and surrounding areas), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Câble-Axion Digital Corp.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Covit Corp. 
                                )
                        )   
                )

        )

    [cambridge-chicago] => Array
        (
            [community_name] => Cambridge (Chicago), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Aṕple.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Avocados.
                                )
                        )   
                )

        )

)

这是我尝试过的方法,但我想知道它是否适用于所有情况。在某些情况下,即使对重音字母进行排序后,其排名也低于非重音字母。

我想知道我应该在下面的代码中做些什么更改,以便重音字母应该像没有重音一样排序。

foreach ($array as &$locality) {
    usort($locality['areas'], function ($a, $b) {
        // return $a['name'][0] <=> $b['name'][0];
        return iconv('UTF-8', 'ISO-8859-8//TRANSLIT', $a['name'][0]) <=> iconv('UTF-8', 'ISO-8859-8//TRANSLIT', $b['name'][0]);
    });
}

您可以使用 Normalizer 从变音符号中拆分字符并在之后删除它们以获得 'base'-字符。

function stripDiacritics(string $string): string {
    return preg_replace(
        '/[\x{0300}-\x{036f}]/u',
        '',
        Normalizer::normalize($string , Normalizer::FORM_D)
    );
}

foreach ($array as &$locality) {
    usort($locality['areas'], function ($a, $b) {
        return stripDiacritics($a['name'][0]) <=> stripDiacritics($b['name'][0]);
    });
}    

工作example

here.

中删除

下次使用 var_export,这样我们就可以使用您的数组来测试代码:)

diacritics 的列表(\x{0300}-\x{036f} 的来源)。

使用国际整理器:

$arr = [
  ['key' => 'Avocado'],
  ['key' => 'Aṕple'],
];

$c = new Collator('root');
usort(
    $arr,
    function($a, $b) use($c){
        return $c->compare($a['key'], $b['key']);
    }
);
var_dump($arr);

输出:

array(2) {
  [0]=>
  array(1) {
    ["key"]=>
    string(7) "Aṕple"
  }
  [1]=>
  array(1) {
    ["key"]=>
    string(7) "Avocado"
  }
}

其中 'root' 使用一组默认规则,这些规则似乎会根据需要忽略重音,但您可以为 language-specific 排序顺序指定实际区域设置。