1

我有 3 个数组表示按日期分组的数据计数$dummyArray1 = array('year' => array('month' => 'count'));

$dummyArray1 = array(
    '2012' => array(
        '1' => 13,
        '2' => 22,
        '3' => 18,
        '5' => 19
     ),
    '2011' => array(
        '1' => '4',
        '3' => 13
     )
);

$dummyArray2 = array(
    '2012' => array(
        '1' => 44,
        '4' => 34,
        '5' => 38,
        '6' => 29
     ),
    '2011' => array(
        '1' => 9,
        '2' => 11,
        '3' => 1
     )
);

$dummyArray3 = array(
    '2012' => array(
        '2' => 5,
        '4' => 3,
        '6' => 1,
        '8' => 11
     ),
    '2011' => array(
        '3' => 9,
        '4' => 14,
        '9' => 9
     )
);

我需要将它们分组到 1 个数组中,以便每个月我都会获得count每个数组的值。如果特定月份不存在计数数据,则应将其作为值0插入。count

4

1 回答 1

1

如果你做一个函数会更容易:

<?php

function groupMyArray($dummysArray)
{
   $result = array();
   foreach($dummysArray as $dummyArray):
      foreach($dummyArray as $year => $countarray):
          for($i = 1; $i <= 12; $i++):
              if(count($countarray[$i]) > 0){
                  $result[$year] = array($i => $countarray[$i]);
              }else{
                  $result[$year] = array($i => 0);
              }
          endfor;
      endforeach;
   endforeach;

   return $result;
}

好的,知道你必须构造 $dummyArray:

$dummyArray = array(
    $dummyArray1,
    $dummyArray2,
    $dummyArray3
);


$dummyArraygroup = groupMyArray($dummyArray);

?>

于 2012-12-02T01:06:02.563 回答