note 47693 deleted from ref.array by aidan
| From: | aidan@php.net | Date: | Sun, 09 Oct 2005 09:14:15 +0000 |
| Subject: | note 47693 deleted from ref.array by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96732@lists.php.net to get a copy of this message | ||
Note Submitter: zubfatal, root at it dot dk
----
Just a few array functions:
array_avg($arrInput)
Returns the average value of alle numeric values in an array.
<?php
function array_avg($arrInput = NULL) {
if ((!is_array($arrInput)) || (!count($arrInput) > 0)) {
return "ERR: \$arrInput is empty, or not an array.";
}
else {
unset($arrTmp);
foreach($arrInput as $key => $value) {
if ((is_numeric($value)) || (is_float($value)) || (is_int($value))) {
$arrTmp[$key] = $value;
}
}
if (is_array($arrTmp)) { return floatval((array_sum($arrTmp) / count($arrTmp))); }
else { return "ERR: No numeric values found."; }
}
}
?>
array_max($arrInput)
Returns the maximum value of all the numeric values in an array.
<?php
function array_max($arrInput = NULL) {
if ((!is_array($arrInput)) || (!count($arrInput) > 0)) {
return "ERR: \$arrInput is empty, or not an array.";
}
else {
unset($arrTmp);
foreach($arrInput as $key => $value) {
if ((is_numeric($value)) || (is_int($value))) {
$arrTmp[$key] = $value;
}
}
if (is_array($arrTmp)) {
sort($arrTmp, SORT_NUMERIC);
return array_pop($arrTmp);
}
else {
return "ERR: No numeric values found.";
}
}
}
?>
array_min($arrInput)
Just the opposite of array_max ;) - returns the minimum value of all the numeric values in an array.
<?php
function array_min($arrInput = NULL) {
if ((!is_array($arrInput)) || (!count($arrInput) > 0)) {
return "ERR: \$arrInput is empty, or not an array.";
}
else {
unset($arrTmp);
foreach($arrInput as $key => $value) {
if ((is_numeric($value)) || (is_int($value))) {
$arrTmp[$key] = $value;
}
}
if (is_array($arrTmp)) {
rsort($arrTmp, SORT_NUMERIC);
return array_pop($arrTmp);
}
else {
return "ERR: No numeric values found.";
}
}
}
?>
// zubfatal