array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

array problem

Post by danwguy »

what I need to do is add up all of the values of an array where the key is 4 or greater and I have no idea where to even start. I have code that pulls a bunch of data from a mssql table and creates an array with $callable[$row[0]] = $row[1]; Now I need that to stay because each iteration of this file that is pushed through to the database the grouping that I need to do will increase. I.e. if this is the first time this will run I need to add up all the vaules where the key is 4 or greater, on the second run I need to add up all the values where the key is 5 or greater, so changning the array as it's called from the db needs to not change. Any body have any idea on how to go about this? the only thing I have thought was ...

Code: Select all

$count = count($callable);
for($i=4; $i < $count; $i++) {
      $group += $callable[$i];
}
but not sure if that would work or not.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: array problem

Post by Celauran »

Where does the 4, 5, etc come from? Could this be done directly in SQL?

Code: Select all

SELECT SUM(foo) AS total FROM table WHERE id > 4
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: array problem

Post by danwguy »

can't do it from sql, I mean I could but they don't want more query calls to the db, I need to figure out a way to take the array $uncallable and just add up the total values that have a key greater than 4. Here is the current code...

Code: Select all

$uncallable_query = "SELECT NIVABS,count(*) FROM PGM.dbo.C1_PGM_BING
WHERE PRIORITE < 0
GROUP BY NIVABS
ORDER BY NIVABS ASC";

$uncallable_results = mssql_query($uncallable_query);

while($row = mssql_fetch_row($uncallable_results)){
    $uncallable[$row[0]] = $row[1];
}
;
and the $row[0];
is not auto incrementing, in other words it could be 1, 2, 3, 4, 5, 8, 15. But I have an array $uncallable[0] = 1525, $uncallable[2] = 3452, and so on, and what I need is $total = everything equal to or greater than $uncallable[4]. I hope I am making sense here.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: array problem

Post by danwguy »

here is a print_r of the $uncallable array...

Code: Select all

Array ( [0] => 1404 [1] => 8413 [2] => 94 [3] => 7 [4] => 1 [6] => 1 [19] => 1 [21] => 1 [22] => 4 )
so I would need everything with a key of 4 or greater, I need the values added up into one variable.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: array problem

Post by Celauran »

Given that, your original solution would work fine as best I can tell.
Post Reply