note 77151 added to function.array-sum

From: Date: Thu, 16 Aug 2007 23:10:20 +0000
Subject: note 77151 added to function.array-sum
Groups: php.notes 
Request: Send a blank email to php-notes+get-129549@lists.php.net to get a copy of this message
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 ); } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 70.61.33.121 ---- Manual Page -- http://www.php.net/manual/en/function.array-sum.php Edit -- https://master.php.net/note/edit/77151 Del: integrated -- https://master.php.net/note/delete/77151/integrated Del: useless -- https://master.php.net/note/delete/77151/useless Del: bad code -- https://master.php.net/note/delete/77151/bad+code Del: spam -- https://master.php.net/note/delete/77151/spam Del: non-english -- https://master.php.net/note/delete/77151/non-english Del: in docs -- https://master.php.net/note/delete/77151/in+docs Del: other reasons-- https://master.php.net/note/delete/77151 Reject -- https://master.php.net/note/reject/77151 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129549) next »