Re: [VOTE] array_key_first(), array_key_last(), array_value_first(), array_value_last()

From: Date: Sat, 14 Jul 2018 22:07:14 +0000
Subject: Re: [VOTE] array_key_first(), array_key_last(), array_value_first(), array_value_last()
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-102828@lists.php.net to get a copy of this message
A function like array_offset would be convenient as there is no way to access the value of an array entry by its index currently. The workaround is using array_slice, with all the downsides already pointed out. 2018-07-13 20:04 GMT-03:00 Levi Morrison <levim@php.net>: > On Fri, Jul 13, 2018 at 4:50 PM Levi Morrison <levim@php.net> wrote: > > > > On Fri, Jul 13, 2018 at 4:48 PM Levi Morrison <levim@php.net> wrote: > > > > > > On Mon, Jul 9, 2018 at 5:17 AM Woortmann, Enno <enno.woortmann@web.de> > wrote: > > > > > > > > Hi, > > > > > > > > as the discussion got no new contributions I'd like to start the > voting > > > > for the RFC fo add new functions for the handling of outer array > elements. > > > > > > > > https://wiki.php.net/rfc/array_key_first_last > > > > > > > > To have a better separation I split up the vote for the functions. > The > > > > first vote covers the functions to handle keys: array_key_first() and > > > > array_key_last(). The second vote covers the corresponding functions > to > > > > handle the values: array_value_first() and array_value_last(). > > > > > > > > As this RFC adds functions but doesn't change the language syntax a > 50% > > > > + 1 majority is required for both votes. The votes are open until > > > > 2018-07-16. > > > > > > > > The discussion for this RFC is located at > > > > > > > > https://externals.io/message/102245 > > > > > > > > Regards, > > > > > > > > Enno > > > > > > > > > > > > > > > > > > > > -- > > > > PHP Internals - PHP Runtime Development Mailing List > > > > To unsubscribe, visit: > > > > http://www.php.net/unsub.php > > > > > > This entire time I felt like this should be possible in pure PHP. > > > Surely, somewhere, we have this capability already? I searched quite a > > > few functions but didn't find anything. > > > > > > However, the feeling was right. Just moments ago Paul Crovella from > > > Stack Overflow mentioned to me that array_slice uses > > > the terminology > > > offset to refer to the order of the entry rather than > > > its key, so I > > > went looking. As far as I can tell from the [array_slice > > > implementation][1] it will not trigger copies and will be efficient if > > > using -1 to retrieve the last index. We can provide the parameter > > > preserve_keys an argument of true to get both the key > > > and the value. > > > These functions *are* efficiently implementable in user-land! > > > > > > Below is a proof-of-concept for the array_offset > > > function [mentioned > > > by Nicolas Grekas][2] (which by the way, neither the RFC author nor > > > anyone else responded to this suggestion) that is simply a convenience > > > wrapper over array_slice: > > > > > > function array_offset(array $input, int $offset): ?array { > > > $slice = array_slice($input, $offset, 1, true); > > > return count($slice) ? $slice : null; > > > } > > > > > > $assoc = ['one' => 1, 'two' => 2, 'three' > > > => 3]; > > > $packd = range(1, 4); > > > > > > var_dump(array_offset($assoc, -1)); > > > var_dump(array_offset($packd, -1)); > > > > > > var_dump(array_offset($assoc, 0)); > > > var_dump(array_offset($packd, 0)); > > > > > > Of course, the array_slice function can be used to > > > build all of the > > > functions described in the RFC, as well. > > > > > > My new opinion is that no new functions are required and that > > > improving the array_slice documentation is all that > > > is necessary. It > > > currently does not show any examples of it working on associated > > > arrays, which is probably why none of us (many of us experts) realized > > > this through this discussion. This is especially true as "offset" in > > > some other situations really means "key", as in > > > ArrayAccess::offsetGet. > > > > > > [1]: https://github.com/php/php-src/blob/ > b9963969fd088dca6852483fdb1c6b7e1080764d/ext/standard/array.c#L3477-L3577 > > > [2]: https://externals.io/message/102245#102322 > > > > Small correction: Nicolas used the name array_index, not > array_offset. > > Unfortunately I was too hasty: array_slice will iterate over > the > entire input range: > > > https://github.com/php/php-src/blob/b9963969fd088dca6852483fdb1c6b > 7e1080764d/ext/standard/array.c#L3538-L3552 > > We could add an case for when array_slice uses > -1. I wonder how > common this already is. Does anyone have a tool that easily can search > open source code for usages of functions? I am interested in this > regardless of the outcome of this RFC. The documentation for > array_slice should also be updated independently of this RFC as > well. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.internals (#102828) next »