How to calculate average of array in php?

Member

by ryleigh , in category: PHP General , a year ago

How to calculate average of array in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , a year ago

@ryleigh 

You can calculate the average of an array in PHP by summing up all the elements in the array and then dividing the sum by the number of elements in the array. Here's an example:

1
2
3
4
5
6
7
$array = [2, 4, 6, 8, 10];

$sum = array_sum($array);
$count = count($array);
$average = $sum / $count;

echo "The average of the array is: " . $average;


This will output:

1
The average of the array is: 6


Related Threads:

How To Calculate Average Directional Index (ADX) in Swift?
How To Calculate Simple Moving Average (SMA) in Lua?
How To Calculate Average True Range (ATR) in Ruby?
How to calculate average of count per day in mysql?
How To Calculate Average True Range (ATR) using Python?
How to calculate exponential values properly in php?