Page 1 of 1

Associative Array

Posted: Fri Jun 06, 2008 11:36 am
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,

Re: Associative Array

Posted: Fri Jun 06, 2008 12:13 pm
by VladSun

Re: Associative Array

Posted: Sun Jun 08, 2008 11:55 am
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.