30秒学会 PHP 片段 · 2018年4月7日

30秒学会 PHP 片段 – groupBy

Groups the elements of an array based on the given function.

Use call_use_func() with $func on $items to group them based on $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;
}

使用样例

groupBy(['one', 'two', 'three'], 'strlen'); // [3 => ['one', 'two'], 5 => ['three']]