Finding the Sum and Average of the Values in an Array : array_sum « Data Structure « PHP






Finding the Sum and Average of the Values in an Array

 
<?php 
function array_average($array) { 
    $retval = FALSE; 
    
    if(is_array($array) && count($array)) 
        $retval = array_sum($array) / count($array); 
    
    return $retval; 
} 
$scores = array('R' => 8.5, 'Jan' => 9.8, 'Terry' => 8.0); 

printf("<p>There are %d scores, totaling %.2f and averaging %.2f.</p>", 
count($scores), array_sum($scores), array_average($scores)); 

?>
  
  








Related examples in the same category

1.array_sum