/** * Groups a collection by index returned by callback. * * @param Traversable|array $collection * @param callable $callback * @return array */ function group($collection, callable $callback) { InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); $groups = []; foreach ($collection as $index => $element) { $groupKey = $callback($element, $index, $collection); InvalidArgumentException::assertValidArrayKey($groupKey, __FUNCTION__); if (!isset($groups[$groupKey])) { $groups[$groupKey] = []; } $groups[$groupKey][$index] = $element; } return $groups; }
/** * Groups a collection by index returned by callback. * * @param Traversable|array $collection * @param callable $callback * @return array */ function group($collection, $callback) { InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2); $groups = array(); foreach ($collection as $index => $element) { $groupKey = call_user_func($callback, $element, $index, $collection); InvalidArgumentException::assertValidArrayKey($groupKey, __FUNCTION__); if (!isset($groups[$groupKey])) { $groups[$groupKey] = array(); } $groups[$groupKey][$index] = $element; } return $groups; }