PHP array to stars -
i have array values: 7.09, 5.63 , 5.35 , ...
i'm rounding them 0.5 with:
round(x * 2) / 2;
now have 7, 6, 5.5
how can make stars these? max stars 5. need make 5 stars = highest array value, what's then? sorry bad english. don't know should do.
7 - 5 stars
6 - 4.5 stars
5.5 - 4 stars
$marray = array(8,2,5,1); $biggestnumber = max($marray); foreach($marray $index => $star){ $percent = $star / $biggestnumber * 100; $mfinalarray[$index]['number'] = $star; $mfinalarray[$index]['stars'] = $percent / 100 * 5; } print_r($mfinalarray);
output :
array ( [0] => array ( [number] => 8 [stars] => 5 ) [1] => array ( [number] => 2 [stars] => 1.25 ) [2] => array ( [number] => 5 [stars] => 3.125 ) [3] => array ( [number] => 1 [stars] => 0.625 ) )
also if want round numbers, change last line
$mfinalarray[$index]['stars'] = $percent / 100 * 5;
to
$mfinalarray[$index]['stars'] = round($percent / 100 * 5);
example output using round
:
array ( [0] => array ( [number] => 8 [stars] => 5 ) [1] => array ( [number] => 2 [stars] => 1 ) [2] => array ( [number] => 5 [stars] => 3 ) [3] => array ( [number] => 1 [stars] => 1 ) )
Comments
Post a Comment