I'm working on an array structure that will be used as the data source for a report template in a web app.
The data comes from relatively complex SQL queries that return one or many rows as one dimensional associative arrays. In the case of many, they are turned into two dimensional indexed array.
The data is complex and in some cases there is a lot of it. To save trips to the database (which are extremely expensive in this scenario) I'm attempting to get all of the basic arrays (1 and 2 dimension raw database data) and put them, conditionally, into a single, five level deep array. Organizing the data in PHP seems like a better idea than by using where statements in the SQL.
Array Structure
Array of years(
year => array of types(
types => array of information(
total => value,
table => array of data(
index => db array
)
)
)
)
My first question is, is this a bad idea. Are arrays like this appropriate for this situation?
If this would work, how should I go about populating it? My initial thought was shallow to deep, but the more I work on this, the more I realize that it'd be very difficult to abstract out the conditionals that determine where each item goes in the array. So it seems that starting from the most deeply nested data may be the approach I should take.
If this is array abuse, what alternatives exist?
Edit
I went ahead with this approach, learning and changing things as I went. But I ended up with a similar end array. What I learned in regards to the shallow to deep conundrum was this.
I went into the process thinking of the outer arrays as the largest of a set of cups, containing multitudes of smaller cups which in turn contained another multitude of smaller cup until the last set which in my mind was water (this being the indexed array of database values). Arrays are not cups...
The idea fell apart when I started legitimately hand drawing out the array structures and found that the base of these arrays, the water filled tiny cups I had envisioned, represented a heck of a lot more data than those big cups. This lead me to see these deeply nested arrays as towers, with the indexed data representing a base of sorts, tapering up to the tip, which was the containing array.
From there, I figured out that the best way for me to process the data was to gather the bottom data, plug it into it's next layer and then go up the tower plugging each layer into it's next layer.
I'm not sure if I'm right, but I do feel like I understand arrays more now than I did before.
And I'm definitely still not sure if this is the best solution.