2017-12-29 37 views
-1

이는 올해의 배열, 월, 연관 값ksort를 사용하여 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; 
} 

을 반환 내 PHP 함수이며, 출력이 보이는

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 
       ) 

     ) 

) 

하지만 난 그냥 1월 같은 ASC에 달 필요

같은 2 월, 월 등 ??

+1

키에 ksort ($ 출력 [$ 년] [$의 달]를) 대체 할 수있는 것은 Gym_class 또는 체육관 수준이어야한다 잘못이나 체육관은 내가 시작을 건너 얼마나 –

답변

1

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

를 교체합니다.

처음. 정확한 연도 범위를 알고있는 경우 연도와 월이있는 빈 배열을 생성 할 수 있습니다. 이걸로, 당신은 한 달을 절대 놓치지 않을 것입니다. 뭔가 그런 ...

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

둘째. 당신은

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


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

두 번째 솔루션은 트릭을 수행하고 이해할 수 있습니다. – alex

+0

오 후속 질문 하나 어떻게 시작과 끝을 건너 뛸까요? 날짜 범위를 벗어나는 경우 개월? (참고 : Google 차트를 계획 중입니다) - – alex

2

몇 가지 해결책이 있습니다

$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

+0

아 한 후속 질문을 @ 알렉스 그들이 날짜 범위를 벗어나면 끝 개월 ?? (FYI : 나는 Google 차트를 작성 중입니다.) - – alex

+0

보통 검사로 작성하는 동안 배열에 넣지 마십시오. – splash58