繁体   English   中英

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

[英]Using ksort to sort by year and month with php?

这是我的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等?

更换

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函数

array_search函数

解决方案很少。

第一。 如果您确切知道年份范围,则可以生成包含年份和月份的空数组。 在此,您将永远不会错过任何一个月。 像这样

$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];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM