[PHP] Array group and sum values.

2010-05-11 Thread Paul Halliday
I have this:

while ($row = mysql_fetch_array($theData[0])) {

$col1[] = $row[0];
$col2[] = lookup($row[1]); // this goes off and gets the country name.

I then loop through col1 and col2 to produce something like this:

52  ARMENIA
215 CANADA
57  CANADA
261 COLOMBIA
53  EGYPT
62  INDIA
50  INDIA

Is there a way I can group these?

Thanks!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array group and sum values.

2010-05-11 Thread Jim Lucas
Paul Halliday wrote:
 I have this:
 
 while ($row = mysql_fetch_array($theData[0])) {
 
 $col1[] = $row[0];
 $col2[] = lookup($row[1]); // this goes off and gets the country name.
 
 I then loop through col1 and col2 to produce something like this:
 
 52ARMENIA
 215   CANADA
 57CANADA
 261   COLOMBIA
 53EGYPT
 62INDIA
 50INDIA
 
 Is there a way I can group these?
 
 Thanks!
 

Group them??

How about this

while ($row = mysql_fetch_array($theData[0])) {

$col1[lookup($row[1])][] = $row[0];

which, using the data you showed, will give you this


Array
(
[ARMENIA] = Array
(
[0] = 52
)

[CANADA] = Array
(
[0] = 215
[1] = 57
)

[COLOMBIA] = Array
(
[0] = 261
)

[EGYPT] = Array
(
[0] = 53
)

[INDIA] = Array
(
[0] = 62
[1] = 50
)

)

-- 
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array group and sum values.

2010-05-11 Thread Paul Halliday
On Tue, May 11, 2010 at 2:25 PM, Jim Lucas li...@cmsws.com wrote:
 Paul Halliday wrote:
 I have this:

 while ($row = mysql_fetch_array($theData[0])) {

     $col1[] = $row[0];
     $col2[] = lookup($row[1]); // this goes off and gets the country name.

 I then loop through col1 and col2 to produce something like this:

 52    ARMENIA
 215   CANADA
 57    CANADA
 261   COLOMBIA
 53    EGYPT
 62    INDIA
 50    INDIA

 Is there a way I can group these?

 Thanks!


 Group them??

 How about this

 while ($row = mysql_fetch_array($theData[0])) {

    $col1[lookup($row[1])][] = $row[0];

 which, using the data you showed, will give you this


 Array
 (
    [ARMENIA] = Array
        (
            [0] = 52
        )

    [CANADA] = Array
        (
            [0] = 215
            [1] = 57
        )

    [COLOMBIA] = Array
        (
            [0] = 261
        )

    [EGYPT] = Array
        (
            [0] = 53
        )

    [INDIA] = Array
        (
            [0] = 62
            [1] = 50
        )

 )

 --
 Jim Lucas

   Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them.

 Twelfth Night, Act II, Scene V
    by William Shakespeare


I was actually hoping to have them arranged like:

$col1[0] = INDIA
$col2[0] = 112
$col1[1] = CANADA
$col2[1] = 272
...

Thanks.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array group and sum values.

2010-05-11 Thread Jim Lucas
Paul Halliday wrote:
 On Tue, May 11, 2010 at 2:25 PM, Jim Lucas li...@cmsws.com wrote:
 Paul Halliday wrote:
 I have this:

 while ($row = mysql_fetch_array($theData[0])) {

 $col1[] = $row[0];
 $col2[] = lookup($row[1]); // this goes off and gets the country name.

 I then loop through col1 and col2 to produce something like this:

 52ARMENIA
 215   CANADA
 57CANADA
 261   COLOMBIA
 53EGYPT
 62INDIA
 50INDIA

 Is there a way I can group these?

 Thanks!

 Group them??

 How about this

 while ($row = mysql_fetch_array($theData[0])) {

$col1[lookup($row[1])][] = $row[0];

 which, using the data you showed, will give you this


 Array
 (
[ARMENIA] = Array
(
[0] = 52
)

[CANADA] = Array
(
[0] = 215
[1] = 57
)

[COLOMBIA] = Array
(
[0] = 261
)

[EGYPT] = Array
(
[0] = 53
)

[INDIA] = Array
(
[0] = 62
[1] = 50
)

 )

 --
 Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

 Twelfth Night, Act II, Scene V
by William Shakespeare

 
 I was actually hoping to have them arranged like:
 
 $col1[0] = INDIA
 $col2[0] = 112
 $col1[1] = CANADA
 $col2[1] = 272
 ...
 
 Thanks.
 

Well, then take what I gave you and do this:

$group[lookup($row[1])][] = $row[0];

foreach ( $group AS $x = $y )
{
$col1[] = $x;
$col2[] = array_sum($y);
}


In the end you will end up with this

plaintext?php

$data = array(
array(52,   'ARMENIA'),
array(215,  'CANADA'),
array(57,   'CANADA'),
array(261,  'COLOMBIA'),
array(53,   'EGYPT'),
array(62,   'INDIA'),
array(50,   'INDIA'),
);

foreach ( $data AS $row )
{
$group[$row[1]][] = $row[0];
}

print_r($group);

foreach ( $group AS $x = $y )
{
$col1[] = $x;
$col2[] = array_sum($y);
}

print_r($col1);
print_r($col2);









-- 
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array group and sum values.

2010-05-11 Thread Paul Halliday
On Tue, May 11, 2010 at 4:03 PM, Jim Lucas li...@cmsws.com wrote:
 Paul Halliday wrote:
 On Tue, May 11, 2010 at 2:25 PM, Jim Lucas li...@cmsws.com wrote:
 Paul Halliday wrote:
 I have this:

 while ($row = mysql_fetch_array($theData[0])) {

     $col1[] = $row[0];
     $col2[] = lookup($row[1]); // this goes off and gets the country name.

 I then loop through col1 and col2 to produce something like this:

 52    ARMENIA
 215   CANADA
 57    CANADA
 261   COLOMBIA
 53    EGYPT
 62    INDIA
 50    INDIA

 Is there a way I can group these?

 Thanks!



 Group them??

 How about this

 while ($row = mysql_fetch_array($theData[0])) {

    $col1[lookup($row[1])][] = $row[0];

 which, using the data you showed, will give you this


 Array
 (
    [ARMENIA] = Array
        (
            [0] = 52
        )

    [CANADA] = Array
        (
            [0] = 215
            [1] = 57
        )

    [COLOMBIA] = Array
        (
            [0] = 261
        )

    [EGYPT] = Array
        (
            [0] = 53
        )

    [INDIA] = Array
        (
            [0] = 62
            [1] = 50
        )

 )

 --
 Jim Lucas

   Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them.

 Twelfth Night, Act II, Scene V
    by William Shakespeare


 I was actually hoping to have them arranged like:

 $col1[0] = INDIA
 $col2[0] = 112
 $col1[1] = CANADA
 $col2[1] = 272
 ...

 Thanks.


 Well, then take what I gave you and do this:

 $group[lookup($row[1])][] = $row[0];

 foreach ( $group AS $x = $y )
 {
        $col1[] = $x;
        $col2[] = array_sum($y);
 }


 In the end you will end up with this

 plaintext?php

 $data = array(
                array(52,       'ARMENIA'),
                array(215,      'CANADA'),
                array(57,       'CANADA'),
                array(261,      'COLOMBIA'),
                array(53,       'EGYPT'),
                array(62,       'INDIA'),
                array(50,       'INDIA'),
                );

 foreach ( $data AS $row )
 {
        $group[$row[1]][] = $row[0];
 }

 print_r($group);

 foreach ( $group AS $x = $y )
 {
        $col1[] = $x;
        $col2[] = array_sum($y);
 }

 print_r($col1);
 print_r($col2);




Perfect! and a lot simpler than I thought.

Thanks.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array group and sum values.

2010-05-11 Thread Shawn McKenzie
On 05/11/2010 02:17 PM, Paul Halliday wrote:
 On Tue, May 11, 2010 at 4:03 PM, Jim Lucas li...@cmsws.com wrote:
 Paul Halliday wrote:
 On Tue, May 11, 2010 at 2:25 PM, Jim Lucas li...@cmsws.com wrote:
 Paul Halliday wrote:
 I have this:

 while ($row = mysql_fetch_array($theData[0])) {

 $col1[] = $row[0];
 $col2[] = lookup($row[1]); // this goes off and gets the country name.

 I then loop through col1 and col2 to produce something like this:

 52ARMENIA
 215   CANADA
 57CANADA
 261   COLOMBIA
 53EGYPT
 62INDIA
 50INDIA

 Is there a way I can group these?

 Thanks!
 
 

 Group them??

 How about this

 while ($row = mysql_fetch_array($theData[0])) {

$col1[lookup($row[1])][] = $row[0];

 which, using the data you showed, will give you this


 Array
 (
[ARMENIA] = Array
(
[0] = 52
)

[CANADA] = Array
(
[0] = 215
[1] = 57
)

[COLOMBIA] = Array
(
[0] = 261
)

[EGYPT] = Array
(
[0] = 53
)

[INDIA] = Array
(
[0] = 62
[1] = 50
)

 )

 --
 Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

 Twelfth Night, Act II, Scene V
by William Shakespeare


 I was actually hoping to have them arranged like:

 $col1[0] = INDIA
 $col2[0] = 112
 $col1[1] = CANADA
 $col2[1] = 272
 ...

 Thanks.


I would probably do this:

$col1 = $col2 = array();

while ($row = mysql_fetch_array($theData[0])) {
$country = lookup($row[1]);

if(($found = array_search($country, $col1)) !== false) {
$col2[$found] += $row[0];
} else {
$col1[] = $country;
$col2[] = $row[0];
}
}

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array group and sum values.

2010-05-11 Thread Shawn McKenzie
On 05/11/2010 04:09 PM, Shawn McKenzie wrote:
 On 05/11/2010 02:17 PM, Paul Halliday wrote:
 On Tue, May 11, 2010 at 4:03 PM, Jim Lucas li...@cmsws.com wrote:
 Paul Halliday wrote:
 On Tue, May 11, 2010 at 2:25 PM, Jim Lucas li...@cmsws.com wrote:
 Paul Halliday wrote:
 I have this:

 while ($row = mysql_fetch_array($theData[0])) {

 $col1[] = $row[0];
 $col2[] = lookup($row[1]); // this goes off and gets the country 
 name.

 I then loop through col1 and col2 to produce something like this:

 52ARMENIA
 215   CANADA
 57CANADA
 261   COLOMBIA
 53EGYPT
 62INDIA
 50INDIA

 Is there a way I can group these?

 Thanks!



 Group them??

 How about this

 while ($row = mysql_fetch_array($theData[0])) {

$col1[lookup($row[1])][] = $row[0];

 which, using the data you showed, will give you this


 Array
 (
[ARMENIA] = Array
(
[0] = 52
)

[CANADA] = Array
(
[0] = 215
[1] = 57
)

[COLOMBIA] = Array
(
[0] = 261
)

[EGYPT] = Array
(
[0] = 53
)

[INDIA] = Array
(
[0] = 62
[1] = 50
)

 )

 --
 Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

 Twelfth Night, Act II, Scene V
by William Shakespeare


 I was actually hoping to have them arranged like:

 $col1[0] = INDIA
 $col2[0] = 112
 $col1[1] = CANADA
 $col2[1] = 272
 ...

 Thanks.

 
 I would probably do this:
 
 $col1 = $col2 = array();
 
 while ($row = mysql_fetch_array($theData[0])) {
   $country = lookup($row[1]);
 
   if(($found = array_search($country, $col1)) !== false) {
   $col2[$found] += $row[0];
   } else {
   $col1[] = $country;
   $col2[] = $row[0];
   }
 }
 

Although I myself would prefer it to be in this format:

$result = array();

foreach($rows as $row) {
$country = lookup($row[1]);

if(isset($result[$country])) {
$result[$country] += $row[0];
} else {
$result[$country] = $row[0];
}
}

Which would give:

array
(
   INDIA = 112
   CANADA = 272
   //etc...
)

Then to use, just:

foreach($result as $country = $value) {
echo $country . ' ' . $value;  // or whatever
}

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php