Associative Array

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Associative Array

Post by kryles »

Hi,

Alright so I know how to use associative arrays, and the regular arrays. I was wondering though in memory when someone declares an associative array key => value, how does it keep track of the key location?

like when you create a normal array arr[0], it looks at the beginning of arr[] location and works its way through like that but how does it know what location to look for arr['foo']?

or does it simply look through the whole array until it comes across that value? If there is a page on this somewhere you can post the link and I'll take a gander...I just got curious as to how it is treated in memory :D

Thanks,
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Associative Array

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Associative Array

Post by Ambush Commander »

like when you create a normal array arr[0], it looks at the beginning of arr[] location and works its way through like that but how does it know what location to look for arr['foo']?
Correction: that's the way it works with linked lists, and generally has O(n). PHP arrays don't use linked lists. And IIRC, PHP doesn't use anything remotely similar to C's "arrays", which give O(1) access time but require you to know their size during allocation.
Post Reply