sandrob57 Posted July 25, 2007 Share Posted July 25, 2007 This is my array $income = array( 'tax' => array( 'name' => 'Citizen Taxes', 'amount' => '45', 'description' => 'in taxes from your citizens.' ), 'house' => array( 'name' => 'Housing Upkeep', 'amount' => '12', 'description' => 'in housing upkeep costs.' ) ); I need to get the total of amount. Currently I do: $total = 0; foreach ($income as $type => $array) { $total = $total + $income[$type]['amount']; } I feel this is sloppy, and I would like to figure out how to correctly use the array_sum() function. Edit: I should mention none of the tutorials I could find helped me in multi-dimension arrays. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 25, 2007 Share Posted July 25, 2007 http://www.php.net/manual/en/function.array-sum.php Quote Link to comment Share on other sites More sharing options...
sandrob57 Posted July 25, 2007 Author Share Posted July 25, 2007 I looked there, but I don't see anything that tells you how to get the sum of a multi-dimension array. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 25, 2007 Share Posted July 25, 2007 foreach ($income as $type) { foreach ($type as $value){ $total .= $value['amount']; } } Quote Link to comment Share on other sites More sharing options...
jitesh Posted July 25, 2007 Share Posted July 25, 2007 <?php $income = array( 'tax' => array( 'name' => 'Citizen Taxes', 'amount' => '45', 'description' => 'in taxes from your citizens.' ), 'house' => array( 'name' => 'Housing Upkeep', 'amount' => '12', 'description' => 'in housing upkeep costs.' ) ); $amount = 0; foreach($income as $k_type => $a_value){ $amount = $amount + $income[$k_type]['amount']; } echo $amount; ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 25, 2007 Share Posted July 25, 2007 this should do the same without foreach in foreach <?php $income = array( 'tax' => array( 'name' => 'Citizen Taxes', 'amount' => '45', 'description' => 'in taxes from your citizens.' ), 'house' => array( 'name' => 'Housing Upkeep', 'amount' => '12', 'description' => 'in housing upkeep costs.' ) ); foreach($income as $value){ $sum .= array_sum($value['amount']); } ?> Quote Link to comment Share on other sites More sharing options...
deadimp Posted July 25, 2007 Share Posted July 25, 2007 Gah! The operator .= is for string concatentation! Therefore, if you were to have "$x=0; $x.=12; $x.=45; $x.=3894;", $x would then equal "012453894". Use operator += and define your numbers as numbers, not string literals (enclosed in quotes ''). $sum=0; foreach ($income as $e) { $sum+=$e['amount']; } I'm not sure why you all are using array_sum() for this either... $income[] is a one-level array. You're not looking for the sum of a multi-dimensional array, you're looking for the sum of specific indexes in sub-arrays. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 25, 2007 Share Posted July 25, 2007 yeah good call you can also use the classic $value = $value + $extra; but the $value += $extra; is easier to write Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.