|
Posted by gosha bine on 05/15/07 14:55
On 15.05.2007 16:21 Darko wrote:
> On May 15, 4:07 pm, harvey <harvey....@blueyonzders.com> wrote:
>> What is the easiest way to get both the last item name and last item
>> value from an associative array?
>>
>> I can get the value with array_pop() but can't see anything similar
>> for the item's name.
>>
>> Thanks
>
> Associative arrays are usually stored in memory as binary trees,
> therefore there is no such thing as "the last element". If you rethink
> it, it's logical, since if you have an array that has integer indices,
> than you _can_ have the "last element" (the element with the largest
> index), but if you have an associative array, what is "the last"
> element? As far as the array is concerned, they are all equal and you
> retrieve them by their key. If what you think is the last element is
> the element you put the last into the array, then it's something you
> have to take care of while inserting them. If you think of the element
> that has the biggest index lexically than you have to find the way to
> sort them.
>
This is correct (although the proper term would be "hash table", not
"binary tree") for any language but php. Other languages have two
distinct types: "arrays" (for which the order of elements is defined)
and "hashes" (which don't guarantee any particular order of elements).
PHP puts the both in one, therefore php "arrays" are ordered even if
there's no numeric indexes.
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
[Back to original message]
|