Jump to content

Recommended Posts

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?

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.

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").

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.