Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Listing Inventory Items Introducing Associative Arrays

The limits of Associative Arrays?

Can you store TOO much information in Associative Arrays? Is the information in a blog post in an Associative Array? I'm wondering what's the limit or capacity of Associative Arrays or when would you NOT use one.

Thanks!

4 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Phillip;

In addition to what Jack said, as a general rule (your mileage and thoughts may vary) I tend to use arrays vs. database tables when the data is static, say a list of states, cities, countries, makes/models of cars, etc. The time savings in being able to access the data in an array over a table can be significant. For lists that are more dynamic, say a list of blog posts, the list of images in a photo gallery (not the images themselves, but the list), users and their information (email, address, phone, etc.), etc. I create database tables.

Just my thoughts. I can certainly be shown different/better/more efficient ways to code, but that is how I generally do things.

Hope it helps,

Ken

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Philip;

The PHP manual on Strings clearly states that the size limit on a string is 2GB. I could not find a similar limitation in the manual for arrays. I would imagine that it would be quite large and perhaps someone else can provide a quantitative answer.

However, there is clearly a difference between what is possible and what is efficient. Arrays take up memory, plain and simple. PHP does not manage memory for us, there are ways to do memory management in PHP, but it requires proactive steps. Obviously the larger the size of the array, the more processing will need to be done by the server.

Does that make sense?

Ken

Hi Ken,

Thanks for your reply, I think you pretty much summed it up when you said...

"there is clearly a difference between what is possible and what is efficient. Arrays take up memory..." —

So, can you give me an example of when NOT to use an Associative Array? Where something might warrant it, but would be best using something else? Just for my understanding!

Thanks for your help,

Philip

Jack Choi
Jack Choi
11,420 Points

There is a very, very technical reason I can think of for when the associative array may not work well and it's if there are a lot of "hash collisions" with the keys being used (it would have to be cosmically unlucky to have this happen in typical usage). As far as size is concerned, it will depend on your environment (32 vs 64 bit) since I believe this is what affects the number of "buckets" - and the smaller the number of the buckets is the higher the chance of collision...and collision is bad.

If you are interested in the topic, I think this post will explain it better than I ever could in words: http://nikic.github.io/2011/12/28/Supercolliding-a-PHP-array.html