-
-
Save tnrn9b/17165f28e3b28b587a15626b2221d62b to your computer and use it in GitHub Desktop.
php implementation of groupBy, which takes an array and a callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$groupBy = function (array $items, callable $cb) { | |
return array_reduce($items, function ($acc, $item) use ($cb) { | |
$key = $cb($item); | |
$acc[$key][] = $item; | |
return $acc; | |
}, []); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment