I would like to calculate the average score of (at the moment two but increasingly more) values in a table for display. I’m having issues with building a function to properly do this.
I have a table “scores” with columns: id, league_id, week, team, total_pts.
I can get the teams into an array:
Array ( [0] => Team1 [1] => Team2 [2] => Team3 )
And I can get the values from the “scores” table into an array:
Is there a way to pull all the total_pts from the second array where the team = the team to perform an array_sum? I’ve tried various functions with foreach loops and running the first array through an array_walk. I just keep either getting just the last value or all the values individually.
For future use, I also need to count the number of elements returned and divide the total by that amount.
Any advise on how to approach this would be appreciated. I’m not having any luck myself.
<?php
$scores = array(1,4,6,4,3,5,6,5,3,5,6);
$score_count = count($scores);
$score_sum = array_sum($scores);
$mean_average = $score_sum / $score_count;
echo "<p>The Mean Average of the scores is $mean_average</p>";
?>
SpacePhoenix, My issue is getting the information into the $scores variable you have.
I may have to rethink how I’m pulling the information from the table.
alexx utza, that gives me the total for Team1. I need to run all the teams through. I’ll try working that into a function where I can use ‘Team1’ as a variable.
What’s your table structure for the tables concerned? It may be possible to shift at least some of the calculations over to MySQL (have MySQL do some of the math).