1

How can calculate this array:

$ratings = [
    1 => 220,
    2 => 31,
    3 => 44,
    4 => 175,
    5 => 3188
];

To a number (the average vote) like:

4

or

3.5
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
FooBar
  • 5,752
  • 10
  • 44
  • 93
  • 1
    Neither 4 or 3.5 is the average of the ratings you posted. – Daan Jan 19 '15 at 15:39
  • @Daan, not part of the question. – FooBar Jan 19 '15 at 15:44
  • You should be more clearer. Why do you want 4 or 3.5 returned? – Daan Jan 19 '15 at 15:45
  • Think a bit about it: what exactly do you want to compute? Start with an item without any ratings. Add a rating, let's say 3 stars. What is average now? Why? Add another rating, let's say 4 stars. What is the average rating now and why? Iterate a couple of times more and find the formula yourself, it's not that difficult. – axiac Jan 19 '15 at 15:48

3 Answers3

3

The basic way to calculate the average is simply to add everything up, and divide it by the total number of values, so:

$total = array_sum($ratings);
$avg = $total/count($ratings);
printf('The average is %.2f', $avg);

The same logic applies to your values, only you need the average rating, so let's get the total number "rating points" that were given, and divide them by the total number of votes:

$totalStars = 0;
$voters = array_sum($ratings);
foreach ($ratings as $stars => $votes)
{//This is the trick, get the number of starts in total, then
 //divide them equally over the total nr of voters to get the average
    $totalStars += $stars * $votes;
}
printf(
    '%d voters awarded a total of %d stars to X, giving an average rating of %.1f',
    $voters,
    $totalStars,
    $totalStars/$voters
);

As you can see here, the output is:

3658 voters awarded a total of 17054 stars to X, giving an average rating of 4.7

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

Divide the total numbers of stars to the total number of votes:

$average = array_sum(array_map(
        function($nbStars, $howManyVotes) {
            return $nbStars * $howManyVotes;
        },
        array_keys($ratings),         // the stars: 1, 2, ... 5
        array_values($ratings)        // the votes for each nb. of stars
    )) / array_sum(array_values($ratings));
axiac
  • 68,258
  • 9
  • 99
  • 134
  • i belive that this will work for you guys $result['total_star'] = $this->db->select_sum('course_rating_start')->where('course_id',$course_id)->get('course_rating_review')->row(); $result['total_rating'] = $this->db->where('course_id',$course_id)->count_all_results('course_rating_review'); $avg = $total_star/$total_rating; – abubakkar tahir Jan 25 '20 at 11:12
-1

Add 220 times a rating of 1, 31 times a rating of 2 and so on. Then divide by the total.

<?php

$ratings = Array (
    1 => 220,
    2 => 31,
    3 => 44,
    4 => 175,
    5 => 3188
);

$max = 0;
$n = 0;
foreach ($ratings as $rate => $count) {
    echo 'Seen ', $count, ' ratings of ', $rate, "\n";
    $max += $rate * $count;
    $n += $count;
}

echo 'Average rating: ', $max / $n, "\n";

?>
Karel Kubat
  • 1,635
  • 11
  • 12