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

From: Date: Wed, 31 May 2023 08:49:27 +0000
Subject: Re: [RFC] [Discussion] Add new function `array_group`
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-120485@lists.php.net to get a copy of this message
On Wed, 31 May 2023 at 10:30, Aleksander Machniak <alec@alec.pl> wrote: > > On 30.05.2023 13:34, Boro Sitnikovski wrote: > > 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 > > > > <https://hackage.haskell.org/package/groupBy-0.1.0.0/docs/Data-List-GroupBy.html>. > > > Sorry, I'm sick right now and have no clue, but how does it relate to > https://wiki.php.net/rfc/array_column_results_grouping > > -- > Aleksander Machniak > Kolab Groupware Developer [https://kolab.org] > Roundcube Webmail Developer [https://roundcube.net] > ---------------------------------------------------- > PGP: 19359DC1 # Blog: https://kolabian.wordpress.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > Okay, I will just share my decade-plus-old take on this problem for anyone interested. I find defining the desired array grouping rules is self-explanatory - and can get arbitrarily complex when required - in this format: columnA=>*. The format can get as complex as columnA[columnB][]=>columnC,columnA - and more - and I bet you can decipher what you get from that without much difficulty nor documentation. It's also quite simple to compose your own. Here's the whole source code for the PHP function along with test coverage: https://github.com/laravel/framework/discussions/45638 Cheers

« previous php.internals (#120485) next »