note 113111 added to function.array-sum

From: Date: Fri, 30 Aug 2013 17:37:47 +0000
Subject: note 113111 added to function.array-sum
Groups: php.notes 
Request: Send a blank email to php-notes+get-196131@lists.php.net to get a copy of this message
/** * This function, given a array of numerical values of any depth or organization, * will provide the sum of all of the (optionally non-negative) values. * Basic Idea (easy to customize to your needs) * first, walk the array, if value is_array, recurse * filter for > 0 values * sum * * @param array $arr the array containing values to check * @param boolean $positiveOnly if true, strip out all < 0 values * @return int the sum of all positive values in the container */ function array_sum_recursive(array $arr = array(), $positiveOnly = true) { foreach (array_keys($arr) as $k) { if (is_array($arr[$k])) { // recurse $arr[$k] = array_sum_recursive($arr[$k], $positiveOnly); } if (empty($arr[$k])) { // strip empties of any type (0 not needed for array_sum) unset($arr[$k]); continue; } if ($positiveOnly && floatval($arr[$k]) < 0) { // strip < 0 values (if $positiveOnly) unset($arr[$k]); continue; } } return array_sum($arr); } ---- Server IP: 69.147.83.199 Probable Submitter: 74.134.238.203 ---- Manual Page -- http://php.net/manual/en/function.array-sum.php Edit -- https://master.php.net/note/edit/113111 Del: integrated -- https://master.php.net/note/delete/113111/integrated Del: useless -- https://master.php.net/note/delete/113111/useless Del: bad code -- https://master.php.net/note/delete/113111/bad+code Del: spam -- https://master.php.net/note/delete/113111/spam Del: non-english -- https://master.php.net/note/delete/113111/non-english Del: in docs -- https://master.php.net/note/delete/113111/in+docs Del: other reasons-- https://master.php.net/note/delete/113111 Reject -- https://master.php.net/note/reject/113111 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196131) next »