PHP/Data Structure/array sum — различия между версиями

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

Текущая версия на 10:02, 26 мая 2010

array_sum

   <source lang="html4strict">

<?php

   $grades = array(42,"hello",42);
   $total = array_sum($grades);
   print $total;

?>

 </source>
   
  


Finding the Sum and Average of the Values in an Array

   <source lang="html4strict">

<?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("

There are %d scores, totaling %.2f and averaging %.2f.

",

count($scores), array_sum($scores), array_average($scores)); ?>

 </source>