Hi John,

John Bafford wrote:
Happy New Year, everyone!

I’d like to present the first new PHP RFC for this year, a proposal to add 
functions to easily get the first, last, or an arbitrary key (and value) by 
index from an array, taking advantage of PHP’s property that arrays are ordered 
maps.

RFC: https://wiki.php.net/rfc/array_key_first_last_index
PR: https://github.com/php/php-src/pull/347

How often would such functions be useful? Perhaps they fill a gap, but I'm not sure if it's one that needs filling. array_key_first and array_key_last can already be accomplished in two or so lines of code (four if you make a function), and array_key_index can be implemented in a few lines with a foreach() loop and a counter.

array_key_first and array_key_last seem mostly harmless, at least. I'm not sure the same can be said for array_key_index, since it has O(n) complexity. I worry that it might end up used by people who think it is more efficient than iterating through an array, even though it is not. I would rather we not include this specific function, and avoid potentially disguising the time complexity of key lookup, especially as I can't think of a good use-case for it. As for array_key_first and array_key_last, well, maybe they're useful, I have no strong opinion on them. They might be handy to get the first and last key of an array without moving the internal pointer, so who knows, maybe we should add them.

Thanks.
--
Andrea Faulds
https://ajf.me/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to