I'm working on poll results and I want to fit 4 dynamic numbers in 100%. Sorry if I didn't explain it correctly, I'm native Dutch and I'm having a hard time trying to explain it correctly.
This is what I mean: If my poll results are like this;
The result should be 25, 25, 25, 25.
Is there a way to calculate this? I have all answer counts in a array like this:
$all_values = array($answers_1, $answer_2, $answer_3, $answer_4);
How can I accomplish this in PHP?
Should be easy enough. You can use array_sum and array_map with a custom function:
$sum = array_sum($all_values);
$result = array_map(function ($value) use ($sum) {
return $value * 100 / $sum;
}, $all_values);
You could cast the results before returning, but I don't know what type you want.