Welcome to WebmasterWorld Guest from 54.224.160.42

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

accessing data within array

based on criteria

     
3:54 am on Oct 12, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 9, 2002
posts:861
votes: 0


I'm drawing a complete blank here, though I feel it shouldn't be nearly so hard to pin down.

Ok. So I have a report that needs to be based on a query which will produce perhaps 50-150 records.

I then need to lay those records out in a tabular sort of format, and do some other twisty bits with it - producing averages and such.

Because of the report layout and the data is a bit irregular (not all items will have data in all years) just looping through the query results to lay out in the tabular format isn't going to work readily, at least not without running a subquery to pull every darn record individually. Way too heavy.

So, brainwave - dump the data in an array and pull it out as needed on the page. eg:

Array([0] => Array([itemid] => 1
[year] => 2003
[grosssales] => 36792
[netsales] => 25758)
[1] => Array([itemid] => 1
[year] => 2004
[grosssales] => 107640
[netsales] => 75348)
[2] => Array([itemid] => 1
[year] => 2005
[grosssales] => 77958
[netsales] => 55350)
[3] => Array([itemid] => 1
[year] => 2002
[grosssales] => 120690
[netsales] => 78444)
[4] => Array([itemid] => 2
[year] => 2003
[grosssales] => 80442
[netsales] => 57924)
[5] => Array([itemid] => 2
[year] => 2005
[grosssales] => 103986
[netsales] => 77994)
[6] => Array([itemid] => 3
[year] => 2007
[grosssales] => 41580
[netsales] => 35748)

Question. How do I then access each piece of data within the array as needed?

eg, I need at a particular point to show the gross sales figure for itemid 2 for the year 2003.

help? please?

4:21 pm on Oct 12, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


Each thing you need to do will have to be specific. To access all of the data within the array you will need a loop and the specify your keys (which look static) To total the grosssales something like this
$arr = array(
"0"=> array("itemid" => "1",
"year" => "2003",
"grosssales" => "36792",
"netsales" => "25758"),
"1" => array("itemid" => "2",
"year" => "2003",
"grosssales" => "2",
"netsales" => "75348"),
"2" => array("itemid" => "2",
"year" => "2003",
"grosssales" => "4",
"netsales" => "55350"),
"3" => array("itemid" => "1",
"year" => "2002",
"grosssales" => "120690",
"netsales" => "78444"));

//the total gross
$totalGross = int;

foreach($arr as $tempArr){

if($tempArr["itemid"] == "2" && $tempArr["year"] == 2003){

//sum up the total gross
$totalGross = (int)$tempArr["grosssales"] + (int)$totalGross;

}//if temparr

}//foreach

echo("The total gross sales is $totalGross");

4:22 pm on Oct 12, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


Also, I would recommend storing that data in the mysql table is you can, because then you can just query your mysql database.
9:07 pm on Oct 12, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


* the mysql table IF not is
9:07 pm on Oct 12, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 9, 2002
posts:861
votes: 0


Thanks andrewsmd. I figured that was the way I'd probably need to go - looping through foreach.

I guess I was mainly wondering if there is a more direct/efficient way to search the array than looping through foreach, but looking at it again it's not really heavy code is it?

Not practical to store the data in a table in this case btw - grosssales/netsales is just a simplified example and there are so many factors in the numbers I need to pull that keeping the table up to date would be a bit of a nightmare.

9:42 pm on Oct 12, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


I figured that much. I'm not aware of anything that is more efficient than a foreach. The reason foreach's get bad names is because of people using inefficient code within them. Let's say you write a block that has an unnecessary if else block in it that takes 30 ms to run. Well, if you are iterating through a couple thousand records or so, that can easily slow down your application. If you going through a ton of data, just make sure you write half way decent code and you'll be fine. Post back if you have any other issues, two heads are always better than one.
11:31 pm on Oct 13, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 9, 2002
posts:861
votes: 0


Just popping back to say it's working... and it's downright zippy. :)

thanks again.

8:29 pm on Oct 14, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


Glad to hear it.