Bug #15330 Updated: An efficient way to access the last element of an array is sorely missed
| From: | torben@php.net | Date: | Fri, 01 Feb 2002 20:11:57 +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-78811@lists.php.net to get a copy of this message | ||
ID: 15330
Updated by: torben@php.net
Reported By: hek@loudoun-net.com
Old Status: Open
Status: Bogus
Bug Type: Feature/Change Request
Operating System: N/A
PHP Version: 4.1.1
New Comment:
These functions have been available for a long time and are
well documented in the manual--see the previous comment.
Torben
Previous Comments:
------------------------------------------------------------------------
[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