i have this array $cartSizes[$productid] where $productid is an array of 8 values (1-8). $cartSizes is stored in a $_SESSION array like this $_SESSION['cartSizes'] = $cartSizes; Code (markup): an example of a $_SESSION['cartSizes'] array that has 2 $productid would be: [cartSizes] => Array ( [8061ABL] => Array ( [1] => 0 [2] => 3 [3] => 4 [4] => 5 [5] => 0 [6] => 0 [7] => 0 [8] => 0 )[6105AAOAU] => Array ( [1] => 3 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 ) ) i want to get the sum of $_SESSION['cartSizes']. but this doesn't work...i keep getting "0". sum_array($_SESSION['cartSizes']) Code (markup): what's wrong?
array_sum() is only designed for simple arrays(). If you check in the PHP documentation pages (http://es.php.net/array_sum) and check the comments you will find a couple of ways to make it work with subarrays. Hope it helps
Try something on the lines of: $sums = array(); foreach ($_SESSION as $key => $value){ if (is_array($value)) $sums[] = array_sum($value); } echo array_sum($sums); PHP:
$sums = array(); foreach ($_SESSION['cartSizes'] as $key => $value){ if (is_array($value)) { $sums[] = array_sum($value); } $totItems = array_sum($sums); echo $totItems; } Code (markup): this seems to work ok. you just missed an open brace bracket for the if statement. thanks!
There wasn't one. My IF statement was a one liner, therefore I ignored that brace. Plus, the code was for guide and example over all else. A Reputation would be appreciated Thanks, Jay