note 47693 added to ref.array
| From: | zubfatal at osu1 dot php dot net | Date: | Thu, 25 Nov 2004 13:06:47 +0000 |
| Subject: | note 47693 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-81013@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47693
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47693&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47693&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47693&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47693&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47693&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47693&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47693&report=yes
Search -- http://master.php.net/manage/user-notes.php