Out of a variable number of values I need to calculate the average. The challenge: The average can only be one of the following fixed values (the closest!):
$allowedAverageValues = [0.66, 1, 1.33, 1.66, 2]
I calculate the average as follows:
$randomValues = [1.33, 1, 0.66, 1, 2, 1.33];
$average = array_sum($randomValues)/count($randomValues); // returns 1.22
How can I identify the value in $allowedAverageValues that is closest to $average? So the desired result is 1.33
.