1

I have 3 arrays representing count of data grouped by dates $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
     )
);

I need to group those in 1 array so that for each month I will have count value of each of the arrays. If a count data does not exist in particular month then there should be 0 inserted as count value.

Desired output:

[
  2012 => [
    1 => 57,
    2 => 27,
    3 => 18,
    4 => 37,
    5 => 57,
    6 => 30,
    7 => 0,
    8 => 11,
    9 => 0,
    10 => 0,
    11 => 0,
    12 => 0,
  ],
  2011 => [
    1 => 13,
    2 => 11,
    3 => 23,
    4 => 14,
    5 => 0,
    6 => 0,
    7 => 0,
    8 => 0,
    9 => 9,
    10 => 0,
    11 => 0,
    12 => 0,
  ],
]
1
  • For clarity, could you add an example of your desired output? Thanks. Commented Dec 2, 2012 at 1:03

2 Answers 2

1

Is easer if you make a function:

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

OK, know you have to construct the $dummyArray:

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


$dummyArraygroup = groupMyArray($dummyArray);

?>

Sign up to request clarification or add additional context in comments.

1 Comment

0

Rather than run a 12-item loop for every year in every input array, just declare an array of zero-valued elements as the foundation for each year. Then only iterate over the actual month elements of each year and sum where required. Demo

$defaults = array_fill_keys(range(1, 12), 0);
$result = [];
foreach ([$array1, $array2, $array3] as $array) {
    foreach ($array as $year => $row) {
        $result[$year] ??= $defaults;
        foreach ($row as $month => $v) {
            $result[$year][$month] += $v;
        }
    }
}
var_export($result);

Output:

array (
  2012 => 
  array (
    1 => 57,
    2 => 27,
    3 => 18,
    4 => 37,
    5 => 57,
    6 => 30,
    7 => 0,
    8 => 11,
    9 => 0,
    10 => 0,
    11 => 0,
    12 => 0,
  ),
  2011 => 
  array (
    1 => 13,
    2 => 11,
    3 => 23,
    4 => 14,
    5 => 0,
    6 => 0,
    7 => 0,
    8 => 0,
    9 => 9,
    10 => 0,
    11 => 0,
    12 => 0,
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.