find the average of all values in a map in php

In order to find the average of all values in a map in PHP, you can use the array_sum() function to sum all the values of the map, and then divide by the number of values using the count() function. Here's an example:

main.php
$map = array("a" => 10, "b" => 20, "c" => 30);
$sum = array_sum($map);
$count = count($map);
$average = $sum / $count;
echo "The average value is: " . $average;
161 chars
6 lines

In the above example, we have a map with three key-value pairs. We calculate the sum of all the values using array_sum(), and count the number of values using count(). We then calculate the average by dividing the sum by the count, and finally, print the result using echo.

related categories

gistlibby LogSnag