Re: Array group and sum values.

From: Date: Tue, 11 May 2010 17:35:11 +0000
Subject: Re: Array group and sum values.
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-305093@lists.php.net to get a copy of this message
On Tue, May 11, 2010 at 2:25 PM, Jim Lucas <lists@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.

« previous php.general (#305093) next »