-1

(In between Christmas meals) I am again stuck with the loop through array and group by logic. here is the array I am given

$aTest = Array
(
    Array
        (
            'date' => 2017-05-04,
            'period' => 'period2',
            'category' => 'Indoor room',
            'score' => 1
        ),

    Array
        (
            'date' => 2017-05-04,
            'period' => 'period5',
            'category' => 'Indoor room',
            'score' => 1
        ),

    Array
        (
            'date' => 2017-05-04,
            'period' => 'period2',
            'category' => 'Indoor room',
            'score' => 2
        ),

    Array
        (
            'date' => 2017-05-04,
            'period' => 'period4',
            'category' => 'Indoor room',
            'score' => 1
        ),

     Array
        (
            'date' => 2017-05-03,
            'period' => 'period5',
            'category' => 'Gym Class',
            'score' => 1
        ),

    Array
        (
            'date' => 2017-05-03,
            'period' => 'period1',
            'category' => 'Gym Class',
            'score' => 1
        ),

     Array
        (
            'date' => 2017-05-03,
            'period' => 'period4',
            'category' => 'Indoor room',
            'score' => 1
        )
        );

This time I like group by category and sum the score group by period. Y-axis will be the category and X-axis will be the period. In the end I need this for a google chart

/*period, total indoor, total gym, 'total indoor', 'total gym'*/
array(
['Period1', 0,1,'0','1'],
['Period2', 3,0,'3','0'],
['Period3', 0, 0,'0','0'],
['Period4', 4,0,'4','0'],
['Period5', 1,1,'1','1']
 )

I have this php code:

    foreach ($aTest as $value) {
            //echo $value['period'].' - '.$value['score'].'<br/>';

            if (empty($output[$value]['period']))
                $output[$value]['period'] = ['Period1' => 0, 'Period2' => 0, 'Period3' =>0, 'Period4' => 0, 'Period5' => 0];

   if(empty($output[$value]['category']))
        $output[$value['catgeory']] = ['Gym Class' => 0, 'Indoor room' =>0];

            $output[$value['category']] += $value['score'];
        }

        ksort($output);

but this only totals the score by Category and not by period. I think I need to loop through the periods as well, but how?

1 Answer 1

1
  • You have a wrong logic here.
if (empty($output[$value]['period']))
                $output[$value]['period'] = ['Period1' => 0, 'Period2' => 0, 'Period3' =>0, 'Period4' => 0, 'Period5' => 0];

that $value is an array and you try to check $output[$value]

  • I saw that you don't have any line sum periods Value.

  • I have a solution for your data. What is my code do??

  • Sum score of the period by the category

  • For each merge period and category score to an array using arraySort category to set position of these category scores values
$temp = [];
$output = []; 
foreach($aTest as $value){
  $period = $value['period'];
  $category = $value['category'];

  // Create default values
  if (empty($temp[$period])){
        $temp[$period] = [];
  }
  if(empty($temp[$period][$category])){
      $temp[$period][$category] = 0;
  }
  //Sum score 
  $temp[$period][$category] += $value['score'];
}

//After Forech we have an array with ['period name' => ['category name' => score]]; 
//Sort values of the category change it if you want, you can add more option such as (item type for add '' to values)
$arraySort = [
  "Indoor room", //total indoor,
  "Gym Class", // total gym, 
  "Indoor room", //'total indoor', 
  "Gym Class" //'total gym'
];
foreach($temp as $period => $catsScore){
  $periodItem = [$period];
  foreach($arraySort as $cat){
      $periodItem[] = $catsScore;
  }
  $output[] = $periodItem;
}
Sign up to request clarification or add additional context in comments.

6 Comments

you help me out a lot I think I need your first part of the code you wrote for my actual array. But I don't get your 2nd part of the code $arraySort, here I get arrays in array '[0] => Array ( [0] => period1 [1] => Array ( [Gym Class] => 1 ) [2] => Array ( [Gym Class] => 1 ) ) etc' I wil reread what you have provided, thanks
2nd part i have sort these category because i think some time input category not ordered.
will try it out but your first part is what i like to use superb!
i am trying your second part to sort the output array to a specific for the x-axis (break, period1, period2, lunch, period3, period5, period6, finish)
well I am having a problem sorting the periods to specific order see my post with your 2nd part stackoverflow.com/questions/47997395/…
|

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.