darryldoo Posted September 4, 2008 Share Posted September 4, 2008 Hi All, My first time with multidimensional arrays and im having a little problem: here is my multidimensional array, depending on the result of a sql query the number of sub arrays can vary. Array ( [0] => Array ( [date] => 2008-09-08 [price] => 55 ) [1] => Array ( [date] => 2008-09-09 [price] => 55 ) [2] => Array ( [date] => 2008-09-21 [price] => 55 ) [3] => Array ( [date] => 2008-09-22 [price] => 55 ) [4] => Array ( [date] => 2008-09-23 [price] => 55 ) ) I have been attempting to add up all of the values of [price] and assign them to a variable, eg.. $totalCost but to no avail ??? I would be most greatful if anyone could offer me any assistance. Thanks in advance of an help on this Jacob Quote Link to comment https://forums.phpfreaks.com/topic/122768-sum-of-values-in-a-multidimensional-array/ Share on other sites More sharing options...
genericnumber1 Posted September 4, 2008 Share Posted September 4, 2008 you could do something like <?php $total = 0; foreach($arrays as $array) { $total += $array['price']; } echo $total; ?> Quote Link to comment https://forums.phpfreaks.com/topic/122768-sum-of-values-in-a-multidimensional-array/#findComment-633931 Share on other sites More sharing options...
DarkWater Posted September 4, 2008 Share Posted September 4, 2008 Or: <?php function multi_array_sum($total, $next) { $total += $next['price']; return $total; } $totalCost = array_reduce($your_array, 'multi_array_sum'); Quote Link to comment https://forums.phpfreaks.com/topic/122768-sum-of-values-in-a-multidimensional-array/#findComment-633941 Share on other sites More sharing options...
genericnumber1 Posted September 4, 2008 Share Posted September 4, 2008 you people and your function-calling-function functions! It's messy to read! Quote Link to comment https://forums.phpfreaks.com/topic/122768-sum-of-values-in-a-multidimensional-array/#findComment-633947 Share on other sites More sharing options...
DarkWater Posted September 4, 2008 Share Posted September 4, 2008 I was actually going to write it like this: <?php $totalcost = array_reduce($your_array, create_function('$total, $next', '$total += $next["price"]; return $total;')); ?> That's how I'd do it. Rather not pollute the code with a bunch of functions for simple things. Quote Link to comment https://forums.phpfreaks.com/topic/122768-sum-of-values-in-a-multidimensional-array/#findComment-633950 Share on other sites More sharing options...
genericnumber1 Posted September 4, 2008 Share Posted September 4, 2008 On behalf of any programmers that will read your code in the future: why, oh god, why?! Quote Link to comment https://forums.phpfreaks.com/topic/122768-sum-of-values-in-a-multidimensional-array/#findComment-633952 Share on other sites More sharing options...
DarkWater Posted September 4, 2008 Share Posted September 4, 2008 On behalf of any programmers that will read your code in the future: why, oh god, why?! They made comments for a reason. I only use create_function for simple callbacks. If it's something that gets advanced, I would never use create_function for it. =P Quote Link to comment https://forums.phpfreaks.com/topic/122768-sum-of-values-in-a-multidimensional-array/#findComment-633953 Share on other sites More sharing options...
genericnumber1 Posted September 4, 2008 Share Posted September 4, 2008 Heh, I've done some ambiguous things in my day as well.. but I personally try to avoid them when it will only save me 2-3 lines I hate when I'm reading people's comments more than their code. Of course, I hate it even more when I can't even read my own scripts a few months later, which is why I've committed to doing things the simplest way possible as long as I don't sacrifice too much performance... personal choice I guess. Quote Link to comment https://forums.phpfreaks.com/topic/122768-sum-of-values-in-a-multidimensional-array/#findComment-633957 Share on other sites More sharing options...
sasa Posted September 4, 2008 Share Posted September 4, 2008 or array_sum(array_map(create_function('$a','return $a["price"];'), $test)); Quote Link to comment https://forums.phpfreaks.com/topic/122768-sum-of-values-in-a-multidimensional-array/#findComment-633990 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.