ecopetition Posted September 7, 2010 Share Posted September 7, 2010 Hello, I have the following array: [0] => Array ( [item_name] => Apples [sUM(item_price)] => 19.98 ) [1] => Array ( [item_name] => Chicken Breasts [sUM(item_price)] => 18.94 ) [2] => Array ( [item_name] => Chicken Thighs [sUM(item_price)] => 4.78 ) [3] => Array ( [item_name] => Yoghurts [sUM(item_price)] => 4.56 ) [4] => Array ( [item_name] => Shampoo [sUM(item_price)] => 3.49 ) [5] => Array ( [item_name] => Strawberries [sUM(item_price)] => 2.58 ) [6] => Array ( [item_name] => Raspberries [sUM(item_price)] => 2.58 ) How can I produce a value of the total of all of the "SUM(item_price)" rows? Also, how can I ask it to return the values for the second rows of the first three entries (to return 19.98, 18.94 and 4.78)? Many thanks in advance. Link to comment https://forums.phpfreaks.com/topic/212797-array-sums/ Share on other sites More sharing options...
MatthewJ Posted September 7, 2010 Share Posted September 7, 2010 Assuming the data is in a multidimensional array called $items, this should work $total = 0; $counter = 1; $newarray = array(); //Get total of all items foreach($items as $v) { $total = $total + $v["SUM(item_price)"]; //Put first three in $newarray if($counter < 4) { $newarray[] = $v["SUM(item_price)"]; } $counter++; } Link to comment https://forums.phpfreaks.com/topic/212797-array-sums/#findComment-1108442 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.