Bug #15330 Updated: An efficient way to access the last element of an array is sorely missed

From: Date: Mon, 04 Feb 2002 15:30:39 +0000
Subject: Bug #15330 Updated: An efficient way to access the last element of an array is sorely missed
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-79099@lists.php.net to get a copy of this message
ID: 15330 Updated by: hek@loudoun-net.com Reported By: hek@loudoun-net.com Status: Bogus Bug Type: Feature/Change Request Operating System: N/A PHP Version: 4.1.1 New Comment: Aha! Now I see array_slice() -- much closer to that I was proposing. However, it still does not get you the value you are after directly, but an array (sans original keys) you then have to delve into for your value. Still, closer. Also, I've found the well-hidden information on getting a CVS account, so no need for that pointer now. Thanks again. --HaigEK Previous Comments: ------------------------------------------------------------------------ [2002-02-04 10:03:15] hek@loudoun-net.com Yes, I am aware of end(). It certainly has a purpose and a use, but it isn't at all the same as what I was suggesting. Frankly, I never use the "internal pointer" features of arrays, as they were at first alien to me, and later seemed unreliable when I thought I understood them. Providing altenative methods for ding things in programming language as otherwise accomodating as PHP, seems to me, precisely in character with its philosophy. I would be more than happy to contribute these functions myself, if someone will point me in the right direction to do so. Thank you. --HaigEK ------------------------------------------------------------------------ [2002-02-01 15:11:56] torben@php.net These functions have been available for a long time and are well documented in the manual--see the previous comment. Torben ------------------------------------------------------------------------ [2002-02-01 14:38:56] matslin@orakel.ntnu.no While there is no way to set the internal pointer (afaik) to a given element #, you can set the pointer to the first/last/next/previous item. http://se.php.net/manual/en/function.end.php end -- Set the internal pointer of an array to its last element. -- mats ------------------------------------------------------------------------ [2002-02-01 14:07:08] hek@loudoun-net.com There seems to be no easy and efficient way to access the last element of an array (assuming you don't already know the key to that element). The most concise, though not terribly efficient workaround I have discovered is... $array[array_pop(array_keys($array))] ...which first extracts all the keys of the array, then pops off the last key and uses it as an index into the array. Fairly easy to write and understand (though bulky), but horribly inefficient. Perhaps there is a place for new array_lastkey($array), array_firstkey($array), and array_element_key($array, $n) functions. The first of these functions would simply return the last key in the array (or null if the array is empty). The second would return the first key in the array (or null). In the third, $n would be a numeric index into the array, totally unrelated to the array's keys. A positive number would indicate how far "into" the array to go, a negative number would indicate how far "backward" into the array to go, while 0 or an out of bounds index might either quietly return a null or might throw an error or warning. Some Examples: $array = array( 1 => "One", "two" => "Two", -3 => "Minus Three" ); echo $array[array_firstkey($array)]; // returns "One" because array_firstkey() returns 1 echo $array[array_lastkey($array)]; // returns "Minus Three" because array_lastkey() returns -3 echo $array[array_element_key($array,1)]; // returns "One" because array_element_key() returns 1 echo $array[array_element_key($array,2)]; // returns "Two" because array_element_key() returns "two" echo $array[array_element_key($array,-2)]; // returns "Two" because array_element_key() returns "two" echo $array[array_element_key($array,-1)]; // returns "Minus Three" because array_element_key() returns -3 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=15330&edit=1

« previous php.dev (#79099) next »