php array percent 1

php array percent

$total = array_sum($share);

$share = array_map(function($hits) use ($total) {
   return round($hits / $total * 100, 1) . '%';
}, $share);

Here is what the above code is Doing:
1. We’re using array_map to iterate over the $share array.
2. We’re using a closure to calculate the percentage of each value in the array.
3. We’re using the $total variable inside the closure.
4. We’re returning the percentage value.

Similar Posts