|
Posted by Darko on 05/15/07 14:21
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.
The one thing that you could do that crosses my mind however, which is
a stupid thing, I must admit, and which looks like something you would
get the results you are looking for, would be to do ob_start(), then
print_r(), then ob_get_contents(), then ob_end_clean(), and parse the
output. But as I said, it doesn't look too nice to me.
Navigation:
[Reply to this message]
|