简体   繁体   中英

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

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??

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

array_search function

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

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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