# Thread: Calculating Average of Certain Array Elements

1. ## Calculating Average of Certain Array Elements

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:
PHP Code:
``` Array ( [0] => Team1 [1] => Team2 [2] => Team3 )  ```
And I can get the values from the "scores" table into an array:
PHP Code:
```  Array ( [0] => Array ( [id] => 1 [league_id] => 79 [week] => 1 [team] => Team1 [total_pts] => 120 ) [1] => Array ( [id] => 4 [league_id] => 79 [week] => 2 [team] => Team1 [total_pts] => 100 ) [2] => Array ( [id] => 2 [league_id] => 79 [week] => 1 [team] => Team2 [total_pts] => 118 ) [3] => Array ( [id] => 5 [league_id] => 79 [week] => 2 [team] => Team2 [total_pts] => 112 ) [4] => Array ( [id] => 3 [league_id] => 79 [week] => 1 [team] => Team3 [total_pts] => 106 ) [5] => Array ( [id] => 6 [league_id] => 79 [week] => 2 [team] => Team3 [total_pts] => 114 )  ```
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.

Thanks
- Craig

2. PHP Code:
``` <?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>";?> ```

3. PHP Code:
``` \$arr = array ( 0 => Array ( 'id' => 1,'league_id'=> 79,'week' => 1,'team' => 'Team1','total_pts' => 120 ),                1 => Array ( 'id' => 4,'league_id'=> 79,'week' => 2,'team' => 'Team1','total_pts' => 100 ),                2 => Array ( 'id' => 2,'league_id'=> 79,'week' => 1,'team' => 'Team2','total_pts' => 118 ),                3 => Array ( 'id' => 5,'league_id'=> 79,'week' => 2,'team' => 'Team2','total_pts' => 112 ),                 4 => Array ( 'id' => 3,'league_id'=> 79,'week' => 1,'team' => 'Team3','total_pts' => 106 ),                 5 => Array ( 'id' => 6,'league_id'=> 79,'week' => 2,'team' => 'Team3','total_pts' => 114 )); \$points = array(); foreach (\$arr as \$value){     if (\$value['team'] == 'Team1'){         \$points[] = \$value['total_pts'];     } } echo array_sum(\$points);  ```

4. 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.

5. I used the following code:
PHP Code:
``` \$team = Array ( [0] => Team1 [1] => Team2 [2] => Team3 );  \$all_scores = Array ( [0] => Array ( [id] => 1 [league_id] => 79 [week] => 1 [team] => Team1 [total_pts] => 120 ) [1] => Array ( [id] => 4 [league_id] => 79 [week] => 2 [team] => Team1 [total_pts] => 100 ) [2] => Array ( [id] => 2 [league_id] => 79 [week] => 1 [team] => Team2 [total_pts] => 118 ) [3] => Array ( [id] => 5 [league_id] => 79 [week] => 2 [team] => Team2 [total_pts] => 112 ) [4] => Array ( [id] => 3 [league_id] => 79 [week] => 1 [team] => Team3 [total_pts] => 106 ) [5] => Array ( [id] => 6 [league_id] => 79 [week] => 2 [team] => Team3 [total_pts] => 114 )  foreach(\$team as \$teams)     {             \$points = array(); foreach (\$all_scores as \$value){     if (\$value['team'] == \$teams){         \$points[] = \$value['total_pts']; } } } \$count = count(\$points); \$score_sum = array_sum(\$points);  \$score_average = \$score_sum/\$count; \$full_array[] = array (\$teams, \$score_average); print_r(\$full_array);  ```
This prints out only the last value in the form I need it.
PHP Code:
``` Array ( [0] => Array ( [0] => Team3 [1] => 110 ) )  ```
Probably something in my foreaches. I'll screw around with those and post what I come up with.

6. Originally Posted by Cfennell
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).

7. I was able to get it with the code in my above post. Just moved one of the closing brackets to get the foreaches working correctly.

Thank you for the replys and help.

-Craig

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•