php - 排序不起作用

时间:2018-02-02 10:58:37

标签: php sorting

我有结构数组:

Array
(
    [months] => Array
        (
            [01] => Array
                (
                    [0] => 16
                    [1] => 31
                )

            [02] => Array
                (
                    [0] => 16
                    [1] => 25
                    [2] => 10
                    [3] => 15
                )

        )

)

现在我尝试在循环中使用uasort

        foreach ($dates['months'] as $month) {
            uasort($month, function($a, $b) {
                return $a <=> $b;
            });
        }

但它没有按天(每月的子阵列)排序 - 为什么?

1 个答案:

答案 0 :(得分:2)

您需要在foreach循环中传递$month并引用

foreach ($dates['months'] as &$month) 

uasort()通过引用修改数组,但是修改了另一个数组中的数组,因此修改了几个月,但它们没有保存在$ dates变量中。

循环中的print_r($dates['months']);函数后,uasort()可以轻松检查它。

相关问题