Re: RFC [Discussion] array_column results grouping

From: Date: Tue, 07 Dec 2021 01:43:36 +0000
Subject: Re: RFC [Discussion] array_column results grouping
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-116580@lists.php.net to get a copy of this message
Revision to my recommendation are: 1. signature: array_group( array|object $array, int|string|array $index_key, callable $reducer = null, callable $mapper = null, callable $filter = null, callable $sorter = null, ): array 2. sorter should be executed before filter, because as far as I can think, there is no way to do this by yourself. So the sequence should be: sorter > filter > mapper|reducer > > Better map/filter/reduce primitives in PHP would be dandy, but they have > nothing to do with array grouping. They're a separate operation that should > be composed with grouping. Packing them all into a single function sounds > like a terrible idea. > > If we wanted that... that's called list comprehensions, which I would > support and even tried to rally support for a few years ago, but got met > with a big shrug. :-) Suppose that my boss order me to provide statistical data of students. He wants a brief data companied with zoom button on each row. This buttons displays all fields in which brief data has, plus another zoom button on it's own row. And so on to some level. Additionally He wants the data not just by area division, but also by gender as a whole. // These are reducer $fnAvg = fn() => {/*...*/} $fnQ1 = fn() => {/*...*/} $fnQ2 = fn() => {/*...*/} $fnQ3 = fn() => {/*...*/} $fnLowest = fn() => {/*...*/} $fnHighest = fn() => {/*...*/} $dAvg = array_group($data, "district", $fnAvg); $dQ1 = array_group($data, "district", $fnQ1); $dQ2 = array_group($data, "district", $fnQ2); // ... and 3 others $wAvg = array_group($data, ["district", "ward"], $fnAvg); $wQ1 = array_group($data, ["district", "ward"], $fnQ1); $wQ2 = array_group($data, ["district", "ward"], $fnQ2); // ... and 3 others $cAvg = array_group($data, ["district", "ward", "cluster"], $fnAvg); $cQ1 = array_group($data, ["district", "ward", "cluster"], $fnQ1); $cQ2 = array_group($data, ["district", "ward", "cluster"], $fnQ2); // ... and 3 others $sAvg = array_group($data, "sex", $fnAvg); $sQ1 = array_group($data, "sex", $fnQ1); $sQ2 = array_group($data, "sex", $fnQ2); // ... and 3 others output: $dAvg = [ "district1" => 123, "district2" => 234, ... ]; $dQ1 = [ "district1" => 123, "district2" => 234, ... ]; ... $wAvg = [ "district1" => [ "ward1" => 123, "ward2" => 234, ], "district2" => [ "ward3" => 123, "ward4" => 234, ], ... ]; $wQ1 = [ "district1" => [ "ward1" => 123, "ward2" => 234, ], "district2" => [ "ward3" => 123, "ward4" => 234, ], ... ]; ... $sAvg = [ "female" => 123, "male" => 234, ... ]; $sQ1 = [ "female" => 123, "male" => 234, ... ]; array_group is just a simple handy tool which can manipulate a bunch of data without touching SQL, DQL, query builder, or any producer anymore just to align the output to the need. Best Regards Hendra Gunawan.

« previous php.internals (#116580) next »