Re: [RFC] [Discussion] Add new function `array_group`

From: Date: Wed, 31 May 2023 21:40:20 +0000
Subject: Re: [RFC] [Discussion] Add new function `array_group`
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-120490@lists.php.net to get a copy of this message
> On 31.5.2023, at 09:03, naitsirch@e.mail.de wrote: > > > > Am 30-May-2023 18:34:19 +0200 schrieb andreas@dqxtech.net: >> On Tue, 30 May 2023 at 18:27, Boro Sitnikovski <buritomath@gmail.com> wrote: >>> >>> Hi, >>> >>> Thank you for your thoughts. >>> >>>> I would say the more common desired behavior is the one in your first >>>> example. And even for that we don't have a native function. >>> >>> This Google search might give more insight into the number of discussions about a >>> grouping functionality: `¦/?�Hš;" >>> 0ª¤https://www.google.com/search?q=php+group+elements+site:stackoverflow.com >> >> All of the examples I looked at are asking for the first kind of >> grouping, that can be implemented as in your first example. >> In all the examples, if two items are equal, they end up in the same group. >> >> In your proposed behavior, equal items can end up in distinct groups >> depending on their original position in the source array. >> I don't see any questions or examples that ask for this. >> >> -- Andreas >> > > I regulary have the need for a grouping function, too. But as Andreas already said: only for > the first kind of grouping: > > $array = [ > [ 'id' => 1, 'type' => 'foo' ], > [ 'id' => 2, 'type' => 'foo' ], > [ 'id' => 3, 'type' => 'baz' ], > ]; > > $groups = array_group(fn ($item) => $item['type']); > > echo json_encode( $groups ); > > { > "foo":[ > {"id":1,"type":"foo"}, > {"id":2,"type":"foo"} > ], > "baz":[ > {"id":3,"type":"baz"} > ] > } > > This behaviour would be inline with Javascript's > Array.prototype.group() function [1] and other frontend libs [2][3]. > > I think the proposed solution is a more unlikely need. At least I never needed this kind of > "grouping". > > [1]: > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/group > [2]: https://underscorejs.org/#groupBy > [3]: https://lodash.com/docs/4.17.15#groupBy > > Best regards > Christian > >>> >>>> Your behavior can be implemented in userland like so: >>>> https://3v4l.org/epvHm >>> >>> Correct, but then again, we can also implement >>> array_map/array_filter/etc. in >>> userland :) >>> >>>> I think you need to make a case as to why the behavior you describe >>>> justifies a native function. >>> >>> Similar to my previous answer, but also in general - ease of access and also >>> performance. >>> >>>> E.g. if you find a lot of public php code that does this kind of grouping. >>>> >>>> I personally suspect it is not that common. >>>> >>>> Cheers >>>> Andreas >>>> >>>> >>>> On Tue, 30 May 2023 at 17:08, Boro Sitnikovski <buritomath@gmail.com> wrote: >>>>> >>>>> Hey, >>>>> >>>>> Thanks for the suggestion. >>>>> >>>>> For the previous case in the code, I added these in a Gist to not clutter here >>>>> too much: >>>>> >>>>> 1. The first example corresponds to >>>>> https://gist.github.com/bor0/b5f449bfe85440d96abd933b9f03b310#file-test_manual_group-php >>>>> 2. The second example corresponds to >>>>> https://gist.github.com/bor0/b5f449bfe85440d96abd933b9f03b310#file-test_array_group-php >>>>> 3. Another example, addressing the problem of increasing subsequences is very >>>>> simple with array_group: >>>>> https://gist.github.com/bor0/b5f449bfe85440d96abd933b9f03b310#file-test_array_incr_subseqs-php >>>>> >>>>> Best, >>>>> >>>>> Boro >>>>> >>>>>> On 30.5.2023, at 16:57, Andreas Hennings <andreas@dqxtech.net> wrote: >>>>>> >>>>>> Hello Boro, >>>>>> I think you should include the "expected result" in your code >>>>>> examples. >>>>>> Maybe this is in your patch file, but I don't think we want to look at >>>>>> that for discussion. >>>>>> >>>>>> Cheers >>>>>> Andreas >>>>>> >>>>>> On Tue, 30 May 2023 at 13:35, Boro Sitnikovski <buritomath@gmail.com> >>>>>> wrote: >>>>>>> >>>>>>> Hello all, >>>>>>> >>>>>>> As per the How To Create an RFC instructions, I am sending this e-mail >>>>>>> in order to get your feedback on my proposal. >>>>>>> >>>>>>> I propose introducing a function to PHP core named >>>>>>> array_group. This function takes an array and a function and returns >>>>>>> an array that contains arrays - groups of consecutive elements. This is very similar to >>>>>>> Haskell's groupBy function. >>>>>>> >>>>>>> For some background as to why - usually, when people want to do >>>>>>> grouping in PHP, they use hash maps, so something like: >>>>>>> >>>>>>> ``` >>>>>>> <?php >>>>>>> $array = [ >>>>>>> [ 'id' => 1, 'value' => 'foo' ], >>>>>>> [ 'id' => 1, 'value' => 'bar' ], >>>>>>> [ 'id' => 2, 'value' => 'baz' ], >>>>>>> ]; >>>>>>> >>>>>>> $groups = []; >>>>>>> foreach ( $array as $element ) { >>>>>>> $groups[ $element['id'] ][] = $element; >>>>>>> } >>>>>>> >>>>>>> var_dump( $groups ); >>>>>>> ``` >> > This is an interesting approach, but it does not allow you to group based on the relation between the elements, so for example, how does the first element compare to the second and should they be grouped together. Rather it considers a single element and does the grouping (determine the key in the hash) based on some property of that element.

« previous php.internals (#120490) next »