note 86971 modified in book.array by danbrown
| From: | danbrown@php.net | Date: | Wed, 12 Nov 2008 20:06:24 +0000 |
| Subject: | note 86971 modified in book.array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146462@lists.php.net to get a copy of this message | ||
To calculate the average of a array
<?php
function array_avg($array,$precision="2"){
$a=0;
if(is_array($array)){
foreach($array as $value):
if(!is_numeric($value)){
$a++;
}
endforeach;
if($a==0){
$cuantos=count($array);
return round(array_sum($array)/$cuantos,$precision);
}else{
return "ERROR in function array_avg(): the array contains one or more non-numeric
values";
}
}else{
return "ERROR in function array_avg(): this is a not array";
}
}
?>
--was--
To calculate the average of a array
<?
function array_avg($array,$precision="2"){
$a=0;
if(is_array($array)){
foreach($array as $value):
if(!is_numeric($value)){
$a++;
}
endforeach;
if($a==0){
$cuantos=count($array);
return round(array_sum($array)/$cuantos,$precision);
}else{
return "ERROR in function array_avg(): the array contains one or more non-numeric
values";
}
}else{
return "ERROR in function array_avg(): this is a not array";
}
}
?>
http://php.net/manual/en/book.array.php