1

我得到了一个名为$user_alerts从 SQL 查询生成的多维数组。供您参考,我在这里打印数组:

Array
(
    [154472] => Array
        (
            [3829802] => Array
                (
                    [34438] => 34438
                    [34442] => 34442
                    [34429] => 34429
                    [34443] => 34443
                )

            [3830078] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                    [34429] => 34429
                )

            [3829803] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34429] => 34429
                    [34443] => 34443
                )

        )

    [154554] => Array
        (
            [3831407] => Array
                (
                    [34438] => 34438
                    [34429] => 34429
                    [34443] => 34443
                    [34442] => 34442
                )

            [3831408] => Array
                (
                    [34429] => 34429
                    [34443] => 34443
                    [34442] => 34442
                    [34438] => 34438
                )

        )

    [154551] => Array
        (
            [3831329] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                    [34429] => 34429
                )

        )

    [154477] => Array
        (
            [3830046] => Array
                (
                    [34442] => 34442
                    [34429] => 34429
                    [34438] => 34438
                    [34443] => 34443
                )

            [3829564] => Array
                (
                    [34429] => 34429
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                )

        )

    [154474] => Array
        (
            [3829402] => Array
                (
                    [34429] => 34429
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                )

        )

    [154473] => Array
        (
            [3829377] => Array
                (
                    [34442] => 34442
                    [34429] => 34429
                    [34438] => 34438
                    [34443] => 34443
                )

        )

    [154541] => Array
        (
            [3831211] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34443] => 34443
                    [34429] => 34429
                )

        )

    [154514] => Array
        (
            [3830597] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34429] => 34429
                    [34443] => 34443
                )

        )

    [154513] => Array
        (
            [3830557] => Array
                (
                    [34442] => 34442
                    [34438] => 34438
                    [34429] => 34429
                    [34443] => 34443
                )

        )

    [154493] => Array
        (
            [3830072] => Array
                (
                    [34442] => 34442
                    [34429] => 34429
                    [34438] => 34438
                    [34443] => 34443
                )

            [3829611] => Array
                (
                    [34429] => 34429
                )

        )

    [154491] => Array
        (
            [3829998] => Array
                (
                    [34429] => 34429
                    [34443] => 34443
                    [34442] => 34442
                    [34438] => 34438
                )

        )

)

现在我想按升序对内部数组进行排序。需要排序的示例内部数组如下(第一个内部数组元素)。

 [3829802] => Array
                    (
                        [34438] => 34438
                        [34442] => 34442
                        [34429] => 34429
                        [34443] => 34443
                    )

实际上我想对所有这样的内部数组进行排序。我尝试了很多技巧,但都没有给我想要的结果。谁能帮我解决这个问题?提前致谢。

4

1 回答 1

2

生成数组时是对其进行排序的最佳时机。当您构建数组时,只需调用ksort($array).

如果由于某种原因不可行或不可能,您可以遍历数组并排序:

foreach($array as $key => $value){
    foreach($value as $innerKey => $inner){
        ksort($inner);
        $array[$key][$innerKey] = $inner;
    }
}
于 2013-07-10T12:27:45.403 回答