ID: 15330 Updated by: [EMAIL PROTECTED] -Reported By: [EMAIL PROTECTED] +Reported By: [EMAIL PROTECTED] Status: Bogus Bug Type: Feature/Change Request Operating System: N/A PHP Version: 4.1.1 New Comment:
Did I already reply to this? Please accept my apologies if I did, and have just misplaced the reply. Thank you for the suggested code. Turns out that all along end() does exactly what I've wanted -- I just didn't catch the part at the end of the manual page for it, where it says the return value is the value of the last element in the array. I think I was misled by [EMAIL PROTECTED]'s line: >> end -- Set the internal pointer of an array to its last element. Sorry for all the bother I've been. Still think that the functions I suggested would do no harm in the library, espescially as then they could be even more efficient than if implemented as user functions. But thanks again. --Haig Previous Comments: ------------------------------------------------------------------------ [2002-02-04 16:15:43] [EMAIL PROTECTED] 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 ------------------------------------------------------------------------ [2002-02-04 10:30:39] [EMAIL PROTECTED] 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 ------------------------------------------------------------------------ [2002-02-04 10:03:15] [EMAIL PROTECTED] 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] [EMAIL PROTECTED] 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] [EMAIL PROTECTED] 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 ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/15330 -- Edit this bug report at http://bugs.php.net/?id=15330&edit=1 -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php