2

So far I have three arrays which return values from the database like this (after much formatting)

[0]=>array(
           [0]=>'Apr', [1]=> 1), 
[1]=>array(
           [0]=>'May', [1]=>2), 
[2]=>array(
           [0]=>'Jun', [1]=>23)

The second array with similar values:

[0]=>array(
           [0]=>'Mar', [1]=>22),     
[1]=>array(
           [0]=>'Apr', [1]=> 1), 
[2]=>array(
           [0]=>'May', [1]=>2), 
[3]=>array(
           [0]=>'Jun', [1]=>25)

And the third:

[0]=>array(
           [0]=>'Jan', [1]=>50),     
[1]=>array(
           [0]=>'Feb', [1]=> 20), 
[2]=>array(
           [0]=>'Mar', [1]=>16), 
[3]=>array(
           [0]=>'Jun', [1]=>5)

And this is how I'm trying to make them to be:

[0]=>array(
           [0]=>'Month', [1]=>'Calc1', [2]=>'Calc2', [3]=>'Calc3'),     
[1]=>array(
           [0]=>'Jan', [1]=>0, [2]=>0, [3]=>50), 
[2]=>array(
           [0]=>'Feb', [1]=>0, [2]=>0, [3]=>20), 
[3]=>array(
           [0]=>'Mar', [1]=>0, [2]=>22, [3]=>16),
[4]=>array(
           [0]=>'Apr', [1]=>1, [2]=>1, [3]=>0),
[5]=>array(
           [0]=>'May', [1]=>2, [2]=>2, [3]=>0),
[6]=>array(
           [0]=>'Jun', [1]=>23, [2]=>25, [3]=>5)

Please note how '0' is filled in places where it contains no value. Honestly, this is the most complex array problem I've ever come across and the reason being the difficulty to traverse through the array. I particularly need the array to be this structure because I need to send it to Google Visualization Area Chart.

Please do post if you've found any suggestions for this problem.

Thank you so much in advance :)

@DevZer0 : This is the output I get (with my real data). Please note how the months are repeating:

   array(
        [0] =>
            array(
                  [0] =>'Month'
                  [1] =>'Calc1'
                  [2] =>'Calc2'
                  [3] =>'Calc3'
                 )
        [1] =>
            array(

                  [0] =>'Apr'
                  [1] =>1
                  [2] =>0
                  [3] =>0
                  )
        [2] =>
            array(

                  [0] =>'Jun'
                  [1] =>9
                  [2] =>0
                  [3] =>0
                  )
        [3] =>
            array(

                  [0] =>'Apr'
                  [1] =>0
                  [2] =>1
                  [3] =>0
                  )
        [4] =>
                  array(

                  [0] =>'May'
                  [1] =>0
                  [2] =>2
                  [3] =>0
                  )
        [5] =>
                  array(

                  [0] =>'Jun'
                  [1] =>0
                  [2] =>23
                  [3] =>0
                  )
        [6] =>
                  array(

                  [0] =>'Apr'
                  [1] =>0
                  [2] =>0
                  [3] =>1
                  )
        [7] =>
                  array(

                  [0] =>'May'
                  [1] =>0
                  [2] =>0
                  [3] =>3
                  )
        [8] =>
                  array(

                  [0] =>'Jun'
                  [1] =>0
                  [2] =>0
                  [3] =>27
                  )
)

I would like them concatenated in this format:

    array(
        [0] =>
            array(
                  [0] =>'Month'
                  [1] =>'Calc1'
                  [2] =>'Calc2'
                  [3] =>'Calc3'
                 )
        [1] =>
            array(

                  [0] =>'Apr'
                  [1] =>1
                  [2] =>1
                  [3] =>1
                  )
        [2] =>
            array(

                  [0] =>'May'
                  [1] =>0
                  [2] =>2
                  [3] =>3
                  )

        [3] =>
            array(

                  [0] =>'Jun'
                  [1] =>9
                  [2] =>23
                  [3] =>27
                  )

)

Is there a way to do it? Thank you so much again:)

AyB
  • 11,609
  • 4
  • 32
  • 47
  • @LucM I know it's not ethics to get other people to do your homework. I would have posted what I tried if it had made sense to me atleast. But I had just too much difficulty figuring out how to traverse through this array coz it's not just associative, it's indexed and inside associative. I even tried making one (manually) similar to it and that itself didn't work O.O – AyB Jun 23 '13 at 13:21

1 Answers1

3

This is not a difficult problem, if i understand correctly you have arrays which has month and an integer. lets assume you have the 3 arrays called $array1,$array2,$array3

 $arrays = array($array1, $array2, $array3);
 $final = array();
 $index = 1;
 foreach ($arrays as $array) {
     foreach ($array as $ar) {
          if (!isset($final[$ar[0]])) {
              $final[$ar[0]] = array($ar[0], $index => $ar[1]);
          } else {
              $final[$ar[0]][$index] = $ar[1];
          }
     }
     $index++;
 }

 $final = array_values($final);

 for($x=0; $x < count($final); $x++) {
      for($i=1; $i < 4; $i++) {
          if (!isset($final[$x][$i])) $final[$x][$i] = "0";
      }
 }

 $header = array("Month", "Calc1", "Calc2", "Calc3");
 array_unshift($final, $header);

 var_dump($final); //will have your resulting array
DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • You're so amazing!! >.< Thank you so much. This would have taken me eternity to figure out. Just one more slight question. Please see my updated post. – AyB Jun 23 '13 at 13:18