Page 1 of 1
array problem
Posted: Wed Aug 24, 2011 3:06 pm
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.
Re: array problem
Posted: Wed Aug 24, 2011 3:14 pm
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
Re: array problem
Posted: Wed Aug 24, 2011 3:20 pm
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.
Re: array problem
Posted: Wed Aug 24, 2011 3:38 pm
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.
Re: array problem
Posted: Thu Aug 25, 2011 10:55 am
by Celauran
Given that, your original solution would work fine as best I can tell.