开发者

PHP multidimensional associative array alternative

I currently have a reporting class that pulls a large series of data from my database and formats it into a 4+ dimensional array to be returned. This works fine but I fear it isn't the best way. Should I be using JSON, XML, interfaces, ect...

Basi开发者_开发百科cally what is the best way to return this data?

Here is an example of how I'm using a multidimensional associative array to return the data:

$results[$teacherID][$studentID][$subject][$testID]

and a sample view of the structure:

Array
(
[17520] => Array
    (
        [01356] => Array
           (
                ["Math"] => Array
                     (
                           [0130] => 75.2
                           [0215] => 76.8
                     )
                ["Science"] => Array
                     (
                           [0130] => 85.2
                           [0215] => 72.2
                           [0308] => 96.3
                           [0320] => 68.4
                     )
            )
        [01468] => Array
           (
                ["Math"] => Array
                     (
                           [0130] => 23.2
                           [0215] => 54.8
                     )
                ["Science"] => Array
                     (
                           [0130] => 72.6
                           [0215] => 79.1
                           [0308] => 68.7
                           [0320] => 72.2
                     )
            )
    )

[17522] => Array
    (
        ect...


It would probably be simpler to query the database with the data you need, when you need it. If you are doing things like looping through the data, getting the infromation into an array from the database seems like a perfectly valid solution.


It's really 6 of one, half dozen of another. The big question to be asking isn't necessarily which method to move the data with, but rather what is the desired outcome?

When I deal with large amounts of data that I'm returning back to the user, I nearly always reach for jQuery's Datatables which allow me to sort, page, filter, etc in a manner that's much more pleasing than I could do in a reasonable amount of time. My data goes over JSON via an ajax call which allows me to not only get data a group at a time, but "pipeline" data so that I can load data ahead of and behind my result to decrease database hits.

However, there are times where a grid isn't the end-all solution. Perhaps you're trying to display this in a more customized layout, or simply gather data to be used by other elements in a page. This would certainly be a valid use for arrays or XML, and I think I'd gravitate toward just doing the arrays simply because I'm more used to it....it's a personal solution.

Sometimes making your query more specific is the answer. However, that can also have grave performance affects. Again, since we don't know specifics it's up to you to determine the benefits of grabbing all at once vs the "cost" of doing multiple smaller transactions.


A JSON file would be somewhat shorter, and it would have the added advantage that it could be easily parsed with languages other than PHP. XML would be bulkier (if download time is a concern, which it probably wouldn't be over an intranet) and has a bigger processing overhead, but again it can be easily parsed by languages other than PHP. Providing the data file in a format that is understood by more than one programming language is always a good idea, because while your current format is very easily parsed by PHP with the smallest parsing penalty, PHP is the only language that can understand it. Having it in a portable format such as JSON or XML provides a degree of future proofing should another system that works with the same data but is implemented in a different language be needed some time in the future.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜