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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
*/


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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux