Skip to content

Instantly share code, notes, and snippets.

@jingmian
Created August 11, 2021 05:56
Show Gist options
  • Save jingmian/0f5f07161783a88c9fbfaf54a731434f to your computer and use it in GitHub Desktop.
Save jingmian/0f5f07161783a88c9fbfaf54a731434f to your computer and use it in GitHub Desktop.
问题整理
## $item->{$func}
如果是类,不应该是$item->{$func}()?
function groupBy($items, $func)
{
$group = [];
foreach ($items as $item) {
if ((!is_string($func) && is_callable($func)) || function_exists($func)) {
$key = call_user_func($func, $item);
$group[$key][] = $item;
} elseif (is_object($item)) {
$group[$item->{$func}][] = $item;
} elseif (isset($item[$func])) {
$group[$item[$func]][] = $item;
}
}
return $group;
}
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment