|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-02-01 14:07 UTC] hek at loudoun-net dot 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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 27 20:00:01 2025 UTC |
Well, if you wouldn't worry about using the internal pointers, you'd have an easier time of this. :) You can do what you need very trivially (note that I'm not saying these functions shouldn't be added to PHP, just that this is already easy to do in userland): For instance: function array_first_key($array) { reset($array); return key($array); } function array_last_key($array) { end($array); return key($array); } ...for the *_value() versions, use current() instead of key(). For the index searcher, you can do something like: function array_element_key($array, $element) { if (abs($element) > count($array)) { return false; } if ($element < 0) { for ($i = -1, end($array); $i > $element; $i--, prev($array)); } else { for ($i = 0, reset($array); $i < $element; $i++, next($array)); } return key($array); } ...and so on. Hope this helps until (if) these get added to the language. Email me privately if you want the whole batch of functions. Torben