使用kso​​rt按年份和月份排序?

时间:2017-12-29 11:34:48

标签: php arrays sorting

这是我的php函数,它返回年,月和关联值的数组

function byCalendarMonth($tmpArray) {
    $output = [];
    foreach ($tmpArray as $value) {
        $year = date('Y', strtotime($value['date']));
        $month = date('M', strtotime($value['date']));

        if (empty($output[$year][$month]))
            $output[$year][$month] = ['Indoor' => 0, 'Gym class' => 0];
        // Check for incident intensity
        $output[$year][$month][$value['category']] += $value['score'];
    }

    ksort($output);
    ksort($output[$year][$month]);
    return $output;
}

,输出看起来像

Array
(
    [2016] => Array
        (
            [Dec] => Array
                (
                    [Indoor] => 39
                    [Gym class] => 34
                )

            [Nov] => Array
                (
                    [Indoor] => 56
                    [Gym class] => 41
                )

            [Oct] => Array
                (
                    [Indoor] => 82
                    [Gym class] => 66
                )

            [Sep] => Array
                (
                    [Indoor] => 97
                    [Gym class] => 89
                )

            [Aug] => Array
                (
                    [Indoor] => 74
                    [Gym class] => 78
                )

            [Jul] => Array
                (
                    [Indoor] => 0
                    [Gym class] => 3
                )

            [Jun] => Array
                (
                    [Indoor] => 74
                    [Gym class] => 98
                )

            [May] => Array
                (
                    [Indoor] => 102
                    [Gym class] => 58
                )

            [Apr] => Array
                (
                    [Gym class] => 49
                    [Indoor] => 106
                )

        )

    [2017] => Array
        (

            [Mar] => Array
                (
                    [Indoor] => 67
                    [Gym class] => 53
                )

            [Feb] => Array
                (
                    [Indoor] => 81
                    [Gym class] => 47
                )

            [Jan] => Array
                (
                    [Indoor] => 84
                    [Gym class] => 49
                )

        )

)

但我只需要几个月到ASC,如jan,feb,mar等?

2 个答案:

答案 0 :(得分:2)

替换

ksort($output[$year][$month]);

$month_names = ['Jan','Feb'...];
foreach($output as $year) {
   uksort($year, 
          function($a, $b) use($month_names) {
             return array_search($a, $month_names, true) - array_search($b, $month_names, true);
          }); 
}

uksort function

array_search function

答案 1 :(得分:1)

解决方案很少。

首先。如果您确切知道年份,则可以生成具有数年和数月的空数组。在这里,你永远不会错过任何一个月。像那样......

$years = [];
for ($year = 2015; $year <= 2017; $year++ ) {
  $years[$year] = ['Jan' => [], 'Feb' => [], ...];
}

二。您可以将ksort($ output [$ year] [$ month])替换为

$listOfOrderedMonth = ['Jan', 'Feb', ...];


$result = [];
foreach ($output as $year => $months) {
    foreach ($listOfOrderedMonth as $orderedMonth) {
        $result[$year][$orderedMonth] = $months[$orderedMonth];
    }
}