Skip to content

Instantly share code, notes, and snippets.

@petemcfarlane
Last active April 17, 2017 04:49
Show Gist options
  • Save petemcfarlane/418a7720420b235f954d0816c13fc295 to your computer and use it in GitHub Desktop.
Save petemcfarlane/418a7720420b235f954d0816c13fc295 to your computer and use it in GitHub Desktop.
php implementation of groupBy, which takes an array and a callback
<?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