up vote 4 down vote favorite
1

I'm in the process of creating a PHP data structure based on some data I'm fetching from somewhere.

If I would encode this information in some standard container, like XML or JSON, I would use this structure:

[ {'name': 'Lupin', 'age': '13'}, {'name': 'Igor', 'age': '24'}, ... ];

However I'm a bit unsure what to use in PHP ( Array of Arrays vs Array of Objects ).

One thing is that I don't really care much about readability, but I would like to be able to sort this Array (which can be quite big). For example I might want to sort it for the biggest age, or something else.

So what structure should I use for my data in PHP? I would like some performance comparisons mostly.

link|flag

3  
If things get really big and you want sortability, wouldn't storying the data in a database be a good option? – Peter Ajtai Jul 10 at 9:09
The database is actually a really good idea, even if its just a mysql or sqlite table in memory. – Timothy Jul 10 at 9:16
@Peter Ajtai, the thing with storing in a database is that each time my PHP script is ran from a user, it needs its own private data space, it can't clobber with the entire database (data that other people are manipulating at that same time). However if there's the possibility I could create a 'Temporary Table' which would be accessed only by that user request. Is that something that people do? I don't work much with databases so I don't know. – Luca Matteis Jul 10 at 9:20
How big is "quite big"? – Craig Jul 11 at 3:53

2 Answers

up vote 2 down vote accepted

@Temporary Table: MySQL indeed allows creating temporary tables through CREATE TEMPORARY TABLE. These tables are only visible to the current connection, so be sure not to use persistent connections ;)

link|flag
I ended up using a database, thanks. – Luca Matteis Jul 14 at 9:21
up vote 1 down vote

Although the array is the most straightforward approach, do not forget the SPL Datastructures. Depending on the UseCase, they can perform better than an array.

Also, I would suggest not to think too much about performance until it starts to impact the application in a negative way (you know the premature optimization is the root of all evil thing). Your architecture should first and foremost be maintainable. If you have decided to follow the OOP paradigm, then performance cannot be your most important factor. It wasn't designed with speed in mind. When you are using objects, you are trying to tackle complexity, not boost performance.

I'd say use what works for you now. Then do a few benchmarks and when it performs too slow, optimize.

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.