根据php中的字段对多维数组进行排序

时间:2020-07-08 21:23:14

标签: php arrays sorting multidimensional-array

我有如下所示的多维数组,我想根据php中的[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] => Carry.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Bacteria.
                                )
                        )   
                )

        )

    [cambridge-chicago] => Array
        (
            [community_name] => Cambridge (Chicago), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Responsibility.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Bacteria.
                                )
                        )   
                )

        )

)

这是我要实现的目标:

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

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

                        )

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

        )

    [chicago-and-surrounding-areas] => Array
        (
            [community_name] => Chicago (and surrounding areas), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Bacteria.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Carry.
                                )
                        )   
                )

        )

    [cambridge-chicago] => Array
        (
            [community_name] => Cambridge (Chicago), IL
            [areas] => Array
                (
                    [0] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Bacteria.
                                )
                        )
                    [1] => Array
                        (
                            [name] => Array
                                (
                                    [0] => Responsibility.
                                )
                        )   
                )

        )

)

这是我尝试过的,但我认为还需要做更多。

function cmp($a, $b)
{
    if ($a["name"] == $b["name"]) {
        return 0;
    }
    return ($a["name"] < $b["name"]) ? -1 : 1;
}

usort($response,"cmp");

问题陈述:

我想知道我需要在上面的php代码中进行哪些更改,以便它根据name字段对数组进行排序。与我上面的PHP代码不进行任何排序。只是按原样打印输入。

1 个答案:

答案 0 :(得分:1)

根据您的示例,您实际上是在尝试对areas子数组而不是整个父数组进行排序。因此,您需要依次遍历每个子数组并将它们分别排序。

$array = [
    'chicago' => [
        'community_name' => 'Chicago, IL',
        'areas' => [
            [
                'name' => ['HELLO WORLD.']
            ],
            [
                'name' => ['Hello'],
            ],
            [
                'name' => ['Administration.'],
            ],
        ],
    ],
    'chicago-and-surrounding-areas' => [
        'community_name' => 'Chicago (and surrounding areas), IL',
        'areas' => [
            [
                'name' => ['Carry.']
            ],
            [
                'name' => ['Bacteria.'],
            ],
        ],
    ],
    'cambridge-chicago' => [
        'community_name' => 'Cambridge (Chicago), IL',
        'areas' => [
            [
                'name' => ['Responsibility.']
            ],
            [
                'name' => ['Bacteria'],
            ],
        ],
    ],
];

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

var_dump($array);

Working example