PHP/Data Structure/array sum

Материал из Web эксперт
Перейти к: навигация, поиск

array_sum

 
<?php
    $grades = array(42,"hello",42);
    $total = array_sum($grades);
    print $total;
?>



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)); 
?>