Skip to content

Instantly share code, notes, and snippets.

@tnrn9b
Forked from petemcfarlane/groupBy.php
Created April 17, 2017 04:49
Show Gist options
  • Save tnrn9b/17165f28e3b28b587a15626b2221d62b to your computer and use it in GitHub Desktop.
Save tnrn9b/17165f28e3b28b587a15626b2221d62b 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