473,498 Members | 703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to extract from array


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
7 12276
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
16953
by: Sharon | last post by:
hi, I want to extract a string from a file, if the file is like this: 1 This is the string 2 3 4 how could I extract the string, starting from the 10th position (i.e. "T") and...
6
10490
by: Mohammad-Reza | last post by:
Hi I want to extract icon of an exe file and want to know how. I look at the MSDN and find out that I can use ExtractIconEx() Windows API but in there are some changes to that api in c# I made...
2
1769
by: monomaniac21 | last post by:
hi all i get an error when i try to extract from a mysql_fetch_array variable it says: Warning: extract(): First argument should be an array in... Is this not possible?
8
2814
by: Fabian Braennstroem | last post by:
Hi, I would like to remove certain lines from a log files. I had some sed/awk scripts for this, but now, I want to use python with its re module for this task. Actually, I have two different...
3
17395
by: roopa.v1 | last post by:
Hi, How to assign long to character array and later extract it
8
2578
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
22
4432
Geeza1973
by: Geeza1973 | last post by:
Below is the code I have: <?php include("db.php"); mysql_connect ($host, $user, $pass); mysql_select_db ($database); $result = mysql_query ("SELECT * FROM `highscore` ORDER BY score DESC LIMIT...
5
2830
by: =?Utf-8?B?aWxy?= | last post by:
Hi This is probably fairly simple but I am newish at programming and was wondering if someone can give me some advice on handling the following. I have an array with a large number of elements...
2
2322
by: hutch75 | last post by:
Hi All - chasing down a means to initiate a traceroute, record results, and extract IP addresses in an (array?) Here's what I'm thinking about so far, wonder if anyone's been down this road before...
3
6452
bilibytes
by: bilibytes | last post by:
Hi everyone, I am wondering if it is possible to extract the key values of an array into object variables. lets say i have the following $_REQUEST array: $var_array = array("color" =>...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.