note 74593 added to function.max
| From: | aragonis at ninjacodingmonkey dot co dot uk | Date: | Thu, 19 Apr 2007 08:15:23 +0000 |
| Subject: | note 74593 added to function.max | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125562@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 87.124.64.96
Probable Submitter: 82.5.51.56
----
Manual Page -- http://www.php.net/manual/en/function.max.php
Edit -- https://master.php.net/note/edit/74593
Del: integrated -- https://master.php.net/note/delete/74593/integrated
Del: useless -- https://master.php.net/note/delete/74593/useless
Del: bad code -- https://master.php.net/note/delete/74593/bad+code
Del: spam -- https://master.php.net/note/delete/74593/spam
Del: non-english -- https://master.php.net/note/delete/74593/non-english
Del: in docs -- https://master.php.net/note/delete/74593/in+docs
Del: other reasons-- https://master.php.net/note/delete/74593
Reject -- https://master.php.net/note/reject/74593
Search -- https://master.php.net/manage/user-notes.php