简体   繁体   English

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

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

This is my php function which returns an array of year, month and associative values 这是我的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;
}

and the output looks like 和输出看起来像

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
                )

        )

)

But I just need the months to ASC like jan, feb, mar etc?? 但是我只需要ASC的月份,如jan,feb,mar等?

Replace 更换

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

with

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

array_search function array_search函数

There are few solutions. 解决方案很少。

First. 第一。 If you know exactly range of years, you can generate empty array with years and months. 如果您确切知道年份范围,则可以生成包含年份和月份的空数组。 In this, you will never miss any month. 在此,您将永远不会错过任何一个月。 Something like that... 像这样

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

Second. 第二。 You can replace ksort($output[$year][$month]) to 您可以将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