stevieontario Posted March 9, 2011 Share Posted March 9, 2011 Afternoon Freaks, I'm trying to add up certain variables inside an array, and keep going around in circles. The array is the result of a mysql SELECT query. I select the contents of a database table, then extract them in php using the following code: while ( $row = mysql_fetch_assoc($result)) { extract($row); } var_dump($row) outputs the following: Array ( [var1] => 1193 [var2] => 799 [var3] => 805 [var4] => 0.992547 [var5] => 2010-12-20 [var6] => 20 ) Array ( [var1] => 1193 [var2] => 725 [var3] => 725 [var4] => 1 [var5] => 2010-12-20 [var6] => 20 ) Array ( [var1] => 1193 [var2] => 818 [var3] => 850 [var4] => 0.962353 [var5] => 2010-12-20 [var6] => 20 ) Array ( [var1] => 1194 [var2] => 819 [var3] => 860 [var4] => 0.952326 [var5] => 2010-12-20 [var6] => 21 ) Array ( [var1] => 1194 [var2] => 829 [var3] => 870 [var4] => 0.952874 [var5] => 2010-12-20 [var6] => 21 ) Array ( [var1] => 1194 [var2] => 831 [var3] => 870 [var4] => 0.952874 [var5] => 2010-12-20 [var6] => 21 ) What I want is to add up all the var2 values for each var1. e.g., in the case of var1=1193, this would be 799 + 725+ 818. Of course, there are nearly 2,000 var1s (each one with multiple var2s), and I want my computer to do all the work. So I'm trying to isolate each var1 so I can figure out some loop to do this operation. I've tried a bunch of funny little tricks with min(), but keep going in circles. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/230128-array-structure-problem-probably-elementary/ Share on other sites More sharing options...
Zane Posted March 9, 2011 Share Posted March 9, 2011 There is no reason to use extract.. The best route in storing the data would be something like this while($row = mysql_fetch_array()) $data[] = $row; That way you have one variable full of arrays. Now you can do something like this foreach($data as $index=>$value) { $data[$index]['var1'] += $data[$index]['var2']; } That most likely WILL NOT work, but I hope you get the idea. Quote Link to comment https://forums.phpfreaks.com/topic/230128-array-structure-problem-probably-elementary/#findComment-1185140 Share on other sites More sharing options...
AbraCadaver Posted March 9, 2011 Share Posted March 9, 2011 You would probably be better off using SUM() and GROUP BY in your query. Quote Link to comment https://forums.phpfreaks.com/topic/230128-array-structure-problem-probably-elementary/#findComment-1185141 Share on other sites More sharing options...
stevieontario Posted March 9, 2011 Author Share Posted March 9, 2011 Zanus, thanks. I'll study your suggestion, though I don't see the difference between the result I get from my extract and your first statement. You correctly assumed that var1 is an auto-incremented field, and I was thinking along the lines of your foreach statement. Let me fiddle around with it and see what happens. Abra, thanks as well. I tried using a subquery, but it wouldn't let me get more than one group of var1s ("subquery returns more than one row"). Quote Link to comment https://forums.phpfreaks.com/topic/230128-array-structure-problem-probably-elementary/#findComment-1185174 Share on other sites More sharing options...
stevieontario Posted March 9, 2011 Author Share Posted March 9, 2011 You see, my problem is I have no idea what I'm doing. I got stuck on the subquery (for whatever reason) and that was what had me hung up. So I took it out and grouped by var1 and PRESTO! Problem solved! Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/230128-array-structure-problem-probably-elementary/#findComment-1185176 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.