note 77151 deleted from function.array-sum by levim
| From: | levim@php.net | Date: | Tue, 18 Mar 2014 19:43:43 +0000 |
| Subject: | note 77151 deleted from function.array-sum by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198968@lists.php.net to get a copy of this message | ||
Note Submitter: Coder
----
A much simpler algorithm for array_sub():
<?php
function array_sub($arr) {
if (!is_array($arr) || count($arr) == 0) {
return false;
} else {
// get first element
$base = array_shift($arr);
return $base - array_sum($arr);
}
}
?>
This will return the exact same results that didikdwi's version.
However, didikdwi's result depends on knowing what the first value is. When this likely bug is
removed, the code is:
<?php
function array_sub($arr){
$temp = array_sum($arr);
return ( $temp !== false ? -$temp : false );
}
?>