To find the average of all keys in a map (or associative array) in PHP, you can use the array_keys()
and array_sum()
functions. Here's an example:
main.php240 chars16 lines
This code creates a map with four keys and values, then uses array_keys()
to extract an array of just the keys. It then uses array_sum()
to find the sum of all the keys, and divides that by the count of the keys to get the average. Finally, it prints out the average of all the keys in the map.
gistlibby LogSnag