Last active
April 17, 2017 04:49
-
-
Save petemcfarlane/418a7720420b235f954d0816c13fc295 to your computer and use it in GitHub Desktop.
php implementation of groupBy, which takes an array and a callback
This file contains hidden or 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