I am getting data from my database that's all working i get the right rows. But now i need to sum them and calculate the average.
My current code is:
$new = array();
foreach($testquery as $key){
$new[$key['school']][$key['city_id']][] = array('points'=>$key['points']);
}
My array structure is like this:
[Etelä-Tapiolan lukio] => Array
(
[4] => Array
(
[0] => Array
(
[points] => 5
)
[1] => Array
(
[points] => 5
)
)
)
[Oulunsalo lukio] => Array
(
[8] => Array
(
[0] => Array
(
[points] => 4
)
[1] => Array
(
[points] => 6
)
[2] => Array
(
[points] => 6
)
[3] => Array
(
[points] => 7
)
)
)
[Limingan lukio] => Array
(
[2] => Array
(
[0] => Array
(
[points] => 4
)
[1] => Array
(
[points] => 5
)
[2] => Array
(
[points] => 3
)
[3] => Array
(
[points] => 3
)
[4] => Array
(
[points] => 4
)
[5] => Array
(
[points] => 6
)
[6] => Array
(
[points] => 4
)
[7] => Array
(
[points] => 6
)
[8] => Array
(
[points] => 3
)
[9] => Array
(
[points] => 3
)
[10] => Array
(
[points] => 5
)
[11] => Array
(
[points] => 7
)
)
)
)
There is no limit on array sizes. Now i need to sum up the points and then divide the sum with the number of points each array has.
The output should be:
Etelä-Tapiolan lukio: 5 (Explanation 5+5 = 10/2 = 5)
Oulunsalo lukio: 5,75 (Explanation 4+6+6+7 = 23/4 = 5,75)
Limingan lukio: 4.41 (Explanation 4+5+3+3+4+6+4+6+3+3+5+7 = 53/12 = 4.41)
I also need the ID = [4],[8],[2] number
[Etelä-Tapiolan lukio] => Array
(
[4] => Array
[Oulunsalo lukio] => Array
(
[8] => Array
[Limingan lukio] => Array
(
[2] => Array
The final output is going to be:
<td class="4">Etelä-Tapiolan lukio:><span> 5 </span></td>
<td class="8">Oulunsalo lukio:><span> 5,75 </span></td>
<td class="2">Limingan lukio:><span> 4.41 </span></td>