SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Boston
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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] => [league_id] => 79 [week] => [team] => Team1 [total_pts] => 120 ) [1] => Array ( [id] => [league_id] => 79 [week] => [team] => Team1 [total_pts] => 100 ) [2] => Array ( [id] => [league_id] => 79 [week] => [team] => Team2 [total_pts] => 118 ) [3] => Array ( [id] => [league_id] => 79 [week] => [team] => Team2 [total_pts] => 112 ) [4] => Array ( [id] => [league_id] => 79 [week] => [team] => Team3 [total_pts] => 106 ) [5] => Array ( [id] => [league_id] => 79 [week] => [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. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,910
    Mentioned
    96 Post(s)
    Tagged
    0 Thread(s)
    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>";

    ?>
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $arr = array ( => Array ( 'id' => 1,'league_id'=> 79,'week' => 1,'team' => 'Team1','total_pts' => 120 ),
                   
    => Array ( 'id' => 4,'league_id'=> 79,'week' => 2,'team' => 'Team1','total_pts' => 100 ),
                   
    => Array ( 'id' => 2,'league_id'=> 79,'week' => 1,'team' => 'Team2','total_pts' => 118 ),
                   
    => Array ( 'id' => 5,'league_id'=> 79,'week' => 2,'team' => 'Team2','total_pts' => 112 ), 
                   
    => Array ( 'id' => 3,'league_id'=> 79,'week' => 1,'team' => 'Team3','total_pts' => 106 ), 
                   
    => 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. #4
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Boston
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Boston
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used the following code:
    PHP Code:
    $team = Array ( [0] => Team1 [1] => Team2 [2] => Team3 ); 
    $all_scores = Array ( [0] => Array ( [id] => [league_id] => 79 [week] => [team] => Team1 [total_pts] => 120 ) [1] => Array ( [id] => [league_id] => 79 [week] => [team] => Team1 [total_pts] => 100 ) [2] => Array ( [id] => [league_id] => 79 [week] => [team] => Team2 [total_pts] => 118 ) [3] => Array ( [id] => [league_id] => 79 [week] => [team] => Team2 [total_pts] => 112 ) [4] => Array ( [id] => [league_id] => 79 [week] => [team] => Team3 [total_pts] => 106 ) [5] => Array ( [id] => [league_id] => 79 [week] => [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. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,910
    Mentioned
    96 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cfennell View Post
    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).
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Location
    Boston
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

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