>
> Hello Folks, Thanks a lot for your feedback, as already mentioned in
> the RFC and as mentioned by Rowan too a new function is an option. I
> think that mostly we will go with the new function option.
> I will try to edit the PR to add a new function, does there any
> suggestions/naming conventions for the new function? a colleague
> suggested being `array_group_by` and Hendra already suggested to be
> `array_column_group` which is good too.
It would be better if You add another voting item for this one. In my opinion,
`column` is less important to be included, and `array_group` is sufficiently
descriptive.
`reducer` is one of the candidates to be included. Additionally, there are
`mapper`, `filter`, and `sorter`. So Obviously we need a new function, rather
than modify the old one.
To be clear and to make it simpler, all callbacks operate on the deepest
subgroup.
Signature:
reducer(array|object $item, int|string $index, mixed $cumulative): mixed
mapper(array|object $item): array|object
filter(array|object $item, int|string $index): bool
sorter(array|object $item1, array|object $item2): int
A note in case you are able to implement them:
- `reducer` and `mapper` cannot coexist in a single function call.
- `filter` should be executed before `reducer` or `mapper`.
- `sorter` should be decided whether it executes before or after `reducer`/
`mapper`: `sorter` is more suitable to execute before `filter`, but more
suitable after `mapper`.
A note for consumers:
- `mapper` is designed to operate on the subgroup item, but it can be operated
on the field value of the item.
- filtering can be done inside `reducer` or `mapper`, but the number of
subgroup members is as it is. With dedicated `filter` supplied, we can
decrease the number.
- If You want to filter an intermediate group, You can use `unset` or `if-else`
$group = array_group($people, ["education", "sex"], FN_REDUCER);
foreach ($group as &$eduGroup) {
foreach ($eduGroup as &$sexGroup) {
unset($sexGroup["male"]);
}
}
Not a perfect solution, but at least your code is reduced.
Best Regards
Hendra Gunawan.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php