Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] [RFC][Under Discussion] Add functions array_key_first() and array_key_last()

Posted by Enno Woortmann 
On 26 June 2018 at 08:58, Marc Bennewitz <[email protected]> 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]
Hi David,


thanks for your brainstorming suggestions. My opinion on the options:

> 1. Keys being stored as reference:
>
> $valueOutput = array_first(array $array, ?string &$keyOutput = null);
In my opinion using references to return additional values from a
function is quiet hacky. I think returning a single value leads to
significantly clearer code and thus supports the developer.
> 2. Function will returns value or key, by option (similar to
> array_filter()):
> ​​
> array_first(array $array, int $options = ARRAY_VALUE);
> ​
> array_first(array $array, int $options = ARRAY_KEY); ​
This violates the single responsibility principle and consequently isn't
a good move
> ​3. ​
> F
> ​unction will returns value or key, by boolean argument:
>
> array_first(array $array, bool $returnKeyInsteadOfValue = false);
> array_first(array $array, bool $returnKeyInsteadOfValue = true); ​
Compare 2.
> 4. Specific method to return key instead of value:
>
> array_first(array $array);
> array_first_key(array $array);
This is the RFC just with slightly different function names. I think
using array_key_first and array_value_first (and the corresponding
functions for the last array element) provides a consistent naming.
> 5. Consider that keys could be obtained via array_keys(), so array_first()
> should just care about values:
>
> array_first(array_keys($array));
This solution requires a copy of the array (keys) which extends the time
complexity and the memory usage of the task.

Regards,
Enno
Am 26.06.2018 um 16:42 schrieb Rowan Collins:
> On 26 June 2018 at 08:58, Marc Bennewitz <[email protected]> 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,

As well as Rowan I don't see use cases for fetching keys/values at any
position inside the array for justifying to break up the current
readable function signatures.
Combining multiple use cases and tasks into a single function always
offends the single responsibility principle (compare my previous mail)
and thus should be avoided.

Are there other opinions concerning this topic?
I'd like to finish the discussion soon and move on to the voting phase.

Regards,
Enno

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
On 28.06.2018 at 15:02, Woortmann, Enno wrote:

> Are there other opinions concerning this topic?

In my opinion, there are already too many array functions, so I'm not
fond of adding more to it. That said, I see the usefulness of
array_key_first() and array_key_last(), but not of array_value_first()
and array_value_last(), since these could easily be implemented on top
of the key functions in userland. Also, it would be possible to
implement the suggested array_first() and array_last() on top of the key
functions, with whatever signature the user desires.

Anyhow, thanks for the RFC!

--
Christoph M. Becker

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
On Thu, Jun 28, 2018 at 7:02 AM Woortmann, Enno <[email protected]> wrote:
>
> Am 26.06.2018 um 16:42 schrieb Rowan Collins:
> > On 26 June 2018 at 08:58, Marc Bennewitz <[email protected]> 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,
>
> As well as Rowan I don't see use cases for fetching keys/values at any
> position inside the array for justifying to break up the current
> readable function signatures.
> Combining multiple use cases and tasks into a single function always
> offends the single responsibility principle (compare my previous mail)
> and thus should be avoided.
>
> Are there other opinions concerning this topic?
> I'd like to finish the discussion soon and move on to the voting phase.

I think there has been a lack of discussion about why this is desired.
For me, it's always been implementing certain kinds of iterators that
wrap arrays. This means that functions which return both the key and
value are optimal for me. As I already outlined the functions which
return the key-value pair are the most general and do result in clean
code without drawbacks, at least for this use-case.

Some people do not like this idea. One potential implication this that
maybe their use-case is not the same as mine. What use-cases do other
people have for this? Why are you wanting to grab the first or last
value? What data structure or algorithm is this a bigger part of?

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Am 28.06.2018 um 16:52 schrieb Levi Morrison:
> I think there has been a lack of discussion about why this is desired.
> For me, it's always been implementing certain kinds of iterators that
> wrap arrays. This means that functions which return both the key and
> value are optimal for me. As I already outlined the functions which
> return the key-value pair are the most general and do result in clean
> code without drawbacks, at least for this use-case.
>
> Some people do not like this idea. One potential implication this that
> maybe their use-case is not the same as mine. What use-cases do other
> people have for this? Why are you wanting to grab the first or last
> value? What data structure or algorithm is this a bigger part of?
Hi Levi,

sorry for the long-lasting answer.

I use plain arrays relatively often when I develop caching mechanics for
multiple rather complex entities. I query a set of suitable entites and
store them as a plain array eg. in Redis.
Each request now fetches this preprocessed entities. Sometimes all
entities are required but I also have use cases where I filter/sort the
list of entries and only one entity is interesting (in all cases I
experienced either the first or the last element). This is my initial
use case for this RFC.

Are there other opinions about the necessity of this functions?

Regards,
Enno



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login