By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,084 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,084 IT Pros & Developers. It's quick & easy.

How to extract from array

P: n/a

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

May 15 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On May 15, 4:07 pm, harvey <harvey....@blueyonzders.comwrote:
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.

May 15 '07 #2

P: n/a
On May 15, 10:21 am, Darko <darko.maksimo...@gmail.comwrote:
On May 15, 4:07 pm, harvey <harvey....@blueyonzders.comwrote:
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.
Even arrays with associative indexes have an ordering to them. Try
the following:

$a = array('foo' ='bar', 'baz' ='bop');
print_r($a);

$b = array('baz' ='bop', 'foo' ='bar');
print_r($b);

And they come out in the same order as they were specified. In fact,
the same holds true for numerically indexed arrays -- the last element
isn't necessarily the one with the largest index:

$a = array(2 ='bar', 1 ='foo');
print_r($a);

To answer the OP's question, you can do something like this:

$a = array('foo' ='bar', 'baz' ='bop');
end($a); //set the pointer to the end of the array
$lastKey = key($a);
$lastValue = current($a);

May 15 '07 #3

P: n/a
On 15.05.2007 16:07 harvey 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
Try

list($key, $val) = each(array_slice($a, -1));

replace 'slice' with 'splice' to remove the last element.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 15 '07 #4

P: n/a
On 15.05.2007 16:21 Darko wrote:
On May 15, 4:07 pm, harvey <harvey....@blueyonzders.comwrote:
>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
May 15 '07 #5

P: n/a
In article <11**********************@p77g2000hsh.googlegroups .com>,
ze********@gmail.com says...
$a = array('foo' ='bar', 'baz' ='bop');
end($a); //set the pointer to the end of the array
$lastKey = key($a);
$lastValue = current($a);

Aha - I found end() in the docs but missed the key() command -

The above does the trick nicely for me

thank you.
May 15 '07 #6

P: n/a
In article <46**********************@read.cnntp.org>,
st********@gmail.com says...
On 15.05.2007 16:07 harvey 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

Try

list($key, $val) = each(array_slice($a, -1));

replace 'slice' with 'splice' to remove the last element.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
That looks good too - but in the interests of clarity of code
I've gone with the previous idea.

But thanks anyway its worth playing with.
May 15 '07 #7

P: n/a
harvey kirjoitti:
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.
$val_last = end($array);
// Sets the internal pointer to last element while it retuns the value

$key_last = key($array);
// Returns the key of the element the internal pointer is at
--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 15 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.