On 04/01/2016 17:05, Andrea Faulds wrote:
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.
Personally, I think they would be very handy, if accompanied by array_value_first and array_value_last. The "ordered hash" PHP provides is a very powerful structure, but sometimes you just want to peek at one or other end of the array without any side effects, and it feels weird that there's no neat idiomatic way of doing this.
Incidentally, the ability to define the functions in userland is quite a positive - it means they can be polyfilled and codebases can start making use of them without requiring a major version bump.
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 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.
This argument does seem reasonable - I can't think of many situations where you'd want exactly one key or element, at a known position, in an indexed array. And as soon as you want two keys or elements, you're probably better off using array_keys / array_values to give you a list which can be efficiently randomly accessed.
Regards, -- Rowan Collins [IMSoP] -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php