note 76501 added to function.array-sum
| From: | didikdwi at gmail dot com | Date: | Wed, 18 Jul 2007 13:48:46 +0000 |
| Subject: | note 76501 added to function.array-sum | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128688@lists.php.net to get a copy of this message | ||
This is a simple approach to subtract array, both indexed and associative.
function array_sub($arr) {
if (!is_array($arr) || count($arr) == 0) {
return false;
} else {
// get first element
$base = array_shift($arr);
if (count($arr) == 1) {
// If only one element, return its
return $base;
}
foreach ($arr as $val) {
$base -= $val;
}
return $base;
}
}
// example
$arr1 = array(3, 2, 3);
echo array_sub($arr1); // Output: -2
$arr2 = array(3.5, 2, 3);
echo array_sub($arr2); // Output: -1.5
$arr3 = array('a' => 3, 'b' => 2, 'c' => 3);
echo array_sub($arr3); // Output: -2
----
Server IP: 202.67.10.139
Probable Submitter: 202.69.101.171
----
Manual Page -- http://www.php.net/manual/en/function.array-sum.php
Edit -- https://master.php.net/note/edit/76501
Del: integrated -- https://master.php.net/note/delete/76501/integrated
Del: useless -- https://master.php.net/note/delete/76501/useless
Del: bad code -- https://master.php.net/note/delete/76501/bad+code
Del: spam -- https://master.php.net/note/delete/76501/spam
Del: non-english -- https://master.php.net/note/delete/76501/non-english
Del: in docs -- https://master.php.net/note/delete/76501/in+docs
Del: other reasons-- https://master.php.net/note/delete/76501
Reject -- https://master.php.net/note/reject/76501
Search -- https://master.php.net/manage/user-notes.php