find average number from array
Posted: Thu Nov 16, 2006 11:16 am
How would I find the average number from an array? Basically I have the following array
Thanks!
Code: Select all
array(5, 30, 40, 50);A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
array(5, 30, 40, 50);Code: Select all
function arrayAverage($arr)
{
$size = count($arr);
$tot = 0;
foreach($arr as $val)
$tot += $val;
$retval = ($tot / $size);
return $retval;
}Code: Select all
function getAverage($array) {
return (array_sum($array) / count($array));
}