# arrays - Use a php function to get the sum per category and counter of scores?

For a Google Chart I need to get the Sum of Scores Per Period and a divider which is the sum of Counting a Score per period per sector

```
$inputArray =
[
['period' => 'period6','sector' => 'SI', 'score' => 3],
['period' => 'period4','sector'=> 'B K','score'=> 2],
['period' => 'period2','sector' => 'FS','score' => 2],
['period' => 'period5','sector' => 'DD','score' => ''],
['period' => 'period3','sector' => 'B K','score' => 3],
['period' => 'period4','sector' => 'SI','score' => 0],
['period' => 'period1','sector' => 'B K', 'score' => 0],
['period' => 'period1','sector' => 'SI','score' => 3],
['period' => 'period4','sector' => 'FS','score' => ''],
['period' => 'period2','sector' => 'FS','score' => 2],
['period' => 'period5','sector' => 'DD','score' => 3],
['period' => 'period3','sector' => 'B K','score' => 3],
['period' => 'period4','sector' => 'SI','score' => ''],
['period' => 'period1','sector' => 'B K', 'score' => 1],
['period' => 'period1','sector' => 'SI','score' => ''],
['period' => 'period4','sector' => 'FS','score' => 2],
];
```

Here i loop through the array and first set each sector per period to 0 and for every iteration i add the score. But I also need a counter which just counts the times a score has been set >= 0. I need this because the Google Chart bars = sum(scoresPerCategory per Period) / (#times scores have been given Per Cat Per Period) and group

```
$output = [];
foreach ($inputArray as $key => $value) {
// Initialize Sum per Period =0
//
if (empty($output[$value['period']])) {
$output[$value['period']] = ['SI' => 0, 'B K' => 0, 'FS' => 0, 'DD' => 0];
$output[$value['period']]['score_counter_'.$value['sector']] = 0;
}
// Total Score
$output[$value['period']][$value['sector']] += $value['score'];
// Only score values with with zero or greater
// Just a Counter NOT total score
// +1 Per Sector Per Period
if ($value['score'] > 0)
$output[$value['period']]['score_counter_' . $value['sector']] += 1;
}
print_r($output);
```

But this doesn't give me the right output

```
[period6] => Array
(
[SI] => 3
[B K] => 0
[FS] => 0
[DD] => 0
[score_counter_SI] => 1
)
[period4] => Array
(
[SI] => 0
[B K] => 2
[FS] => 2
[DD] => 0
[score_counter_B K] => 1
[score_counter_FS] => 1
)
[period2] => Array
(
[SI] => 0
[B K] => 0
[FS] => 4
[DD] => 0
[score_counter_FS] => 2
)
[period5] => Array
(
[SI] => 0
[B K] => 0
[FS] => 0
[DD] => 3
[score_counter_DD] => 1
)
[period3] => Array
(
[SI] => 0
[B K] => 6
[FS] => 0
[DD] => 0
[score_counter_B K] => 2
)
[period1] => Array
(
[SI] => 3
[B K] => 1
[FS] => 0
[DD] => 0
[score_counter_B K] => 1
[score_counter_SI] => 1
)
```

Not every array has a score_counter_xx which I expected. I am still struggling with arrays :(

[EDIT]
with my bigger input array the simple counter doesn't seem to work. My simple logic:```
if ($value['score'] > 0)
$output[$value['period']]['score_counter_' . $value['sector']] += 1;
```

puts the same number for every ['score_counter_' . $value['sector']

## Answer

## Solution:

If you're trying to initialize a score_counter_XX entry for every score type, you can do it in this line:

...by adding in the zero counts for those type:

That will initialize those counters.