note 74593 deleted from function.max by bjori
| From: | bjori@php.net | Date: | Fri, 20 Apr 2007 09:13:24 +0000 |
| Subject: | note 74593 deleted from function.max by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125589@lists.php.net to get a copy of this message | ||
Note Submitter: aragonis at ninjacodingmonkey dot co dot uk
Reason: useless
----
Two functions that may be of use to somebody are calculating the delta (max - min) of an array, as
well as the average of all numeric values in an array.
The delta function requires all values in the array to be numeric. The average function will simply
ignore any non-numeric values.
function arrayDelta($numArray) {
if(!isset($numArray)) {
return '';
}
if(!is_array($numArray)) {
return '';
}
foreach($numArray as $n) {
if(!is_numeric($n)) {
return '';
}
}
return max($numArray) - min($numArray);
}
function arrayAverage($numArray) {
if(!isset($numArray)) {
return '';
}
if(!is_array($numArray)) {
return '';
}
$c = count($numArray);
/*
Make sure we don't get divide by zero errors later.
*/
if($c == 0) {
return '';
}
$sum = 0;
foreach($numArray as $n) {
if(is_numeric($n)) {
$sum += $n;
}
}
return $sum / $c;
}