72 lines
2.4 KiB
PHP
72 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Helper;
|
|
class ArraySorter
|
|
{
|
|
/**
|
|
* Groups an array by a given key.
|
|
*
|
|
* Groups an array into arrays by a given key, or set of keys, shared between all array members.
|
|
*
|
|
* Based on {@author Jake Zatecky}'s {@link https://github.com/jakezatecky/array_group_by array_group_by()} function.
|
|
* This variant allows $key to be closures.
|
|
*
|
|
* @param array $array The array to have grouping performed on.
|
|
* @param mixed $key,... The key to group or split by. Can be a _string_,
|
|
* an _integer_, a _float_, or a _callable_.
|
|
*
|
|
* If the key is a callback, it must return
|
|
* a valid key from the array.
|
|
*
|
|
* If the key is _NULL_, the iterated element is skipped.
|
|
*
|
|
* ```
|
|
* string|int callback ( mixed $item )
|
|
* ```
|
|
*
|
|
* @return array|null Returns a multidimensional array or `null` if `$key` is invalid.
|
|
*/
|
|
public static function groupBy(array $array, $key): ?array
|
|
{
|
|
if (!is_string($key) && !is_int($key) && !is_float($key) && !is_callable($key)) {
|
|
return null;
|
|
}
|
|
|
|
$func = (!is_string($key) && is_callable($key) ? $key : null);
|
|
$key2 = $key;
|
|
|
|
// Load the new array, splitting by the target key
|
|
$grouped = [];
|
|
foreach ($array as $value) {
|
|
$key = null;
|
|
|
|
if (is_callable($func)) {
|
|
$key = call_user_func($func, $value);
|
|
} elseif (is_object($value) && property_exists($value, $key2)) {
|
|
$key = $value->{$key2};
|
|
} elseif (isset($value[$key2])) {
|
|
$key = $value[$key2];
|
|
}
|
|
|
|
if ($key === null) {
|
|
continue;
|
|
}
|
|
|
|
$grouped[$key][] = $value;
|
|
}
|
|
|
|
// Recursively build a nested grouping if more parameters are supplied
|
|
// Each grouped array value is grouped according to the next sequential key
|
|
if (func_num_args() > 2) {
|
|
$args = func_get_args();
|
|
|
|
foreach ($grouped as $key => $value) {
|
|
$params = array_merge([$value], array_slice($args, 2, func_num_args()));
|
|
$grouped[$key] = call_user_func_array('array_group_by', $params);
|
|
}
|
|
}
|
|
|
|
return $grouped;
|
|
}
|
|
}
|