Re: Trimming an array while adding up one "column"

From: Date: Thu, 10 Apr 2003 07:07:20 +0000
Subject: Re: Trimming an array while adding up one "column"
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-26982@lists.php.net to get a copy of this message
On Thursday 10 April 2003 05:30, John Krewson wrote: > Here's is a brief overview of what I'm trying to do, and then the code. > I am building an array from a query that contains value fields and then > a count value. Some of these value fields are the same, so I would like > to trim the array of any repeating values, while making sure that the > array items that contain the counts are added up appropriately. One > thing to note is that I can't do this during the initial database query, > but only after the initial array has been created. > > Here is the meat of the code that builds the initial array (I've > slaughtered indention to make it fit better via email): > ... > while($row = mssql_fetch_array($result)) > { > $subResponses_array[$ctr]["code"] = $row[code]; > $subResponses_array[$ctr]["responseCount"] = $row[responseCount]; > $ctr++; > } If I understand you correctly then something like the following should work: foreach ($subResponses_array as $val) { $code = $val['code']; $count = $val['responseCount']; if (isset($sorted['code'])) { $sorted['code'] += $count; } else { $sorted['code'] = $count; } } *** Untested *** use with caution. -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * ------------------------------------------ Search the list archives before you post http://marc.theaimsgroup.com/?l=php-db ------------------------------------------ /* I trust the first lion he meets will do his duty. -- J.P. Morgan on Teddy Roosevelt's safari */

« previous php.db (#26982) next »