Re: [RFC][Under Discussion] Add functions array_key_first()and array_key_last()
| From: | Rowan Collins | Date: | Tue, 26 Jun 2018 14:42:04 +0000 |
| Subject: | Re: [RFC][Under Discussion] Add functions array_key_first()and array_key_last() | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102455@lists.php.net to get a copy of this message | ||
On 26 June 2018 at 08:58, Marc Bennewitz <dev@mabe.berlin> wrote:
> Hi all,
>
> I just want to add some information that I feel it's missing in that
> discussion - I'm sorry if it was mentioned already.
>
> 1. It's already possible to extract the key and value of any index
> position of an array using array_slice: https://3v4l.org/mib99
>
> 2. "array_[key|value]_[first|last]($array)" is in my opinion not a good
> option as it lacks the possibility to extract key/value from any position.
> Something like "array_[key|value]_index($array, $index)" where $index
> would be the index position or negative index position from right.
>
The big advantage of separate functions is that they make the intent clear;
we could combine all four functions into one: array_index(int $position,
bool $key), but "array_key_last($something)" is a lot clearer to read than
"array_index($something, -1, true)". Are there actually use cases for
getting "the 5th key from the end of the array", common enough to make "the
last key in the array" harder to write?
Regards,
--
Rowan Collins
[IMSoP]