php - How do I calculate the average rating of the detail rating? -
my code :
<?php $rating = 5; $rating_detail = '{"3":"1","4":"2"}'; $array_data = json_decode($rating_detail, true); if(array_key_exists($rating, $array_data)) { $value = $array_data[$rating]; if ($value !== false) { $array_data[$rating] = (string)((int)$value + 1); } $rating_detail = json_encode($array_data); } else { $data = substr($rating_detail, 0, -1); $rating_detail = $data.',"'.$rating.'":"1"}'; } echo '<pre>';print_r($rating_detail);echo '</pre>';die(); ?>
if code run, result :
{"3":"1","4":"2","5":"1"}
it's detail rating of store
note :
3 = rating 3, 1 = number of users giving ratings : 1
4 = rating 4, 2 = number of users giving ratings : 2
5 = rating 5, 1 = number of users giving ratings : 1
how calculate average rating of detail rating?
so average rating of detail rating of store between 1 - 5
update
so here :
{"3":"1","4":"2","5":"1"}
based on detail of rating, how calculate rating of store?
try this
$rating = 5; $rating_detail = '{"3":"1","4":"2"}'; $rating_detail = json_decode($rating_detail, true); if(array_key_exists($rating, $rating_detail)) { $rating_detail[$rating] = (string)((int)$rating_detail[$rating] + 1); } else { $rating_detail[$rating]="1"; } $totalstar=$totalrate=0; foreach ($rating_detail $key=>$value){ $totalstar+=$key*$value; $totalrate+=$value; } $average=$totalstar/$totalrate;
it give out put as
Comments
Post a Comment