function array_group_by( array $array, $key )
{
if ( ! is_string( $key ) && ! is_int( $key ) && ! is_float( $key ) && ! is_callable( $key ) ) {
trigger_error( 'array_group_by(): The key should be a string, an integer, or a callback', E_USER_ERROR );
return null;
}
$func = ( is_callable( $key ) ? $key : null );
$_key = $key;
// Load the new array, splitting by the target key
$grouped = array();
foreach ( $array as $value ) {
if ( is_callable( $func ) ) {
$key = call_user_func( $func, $value );
}
else {
$key = $value[ $_key ];
}
$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(array($value), array_slice( $args, 2, func_num_args() ) );
$grouped[ $key ] = call_user_func_array( 'array_group_by', $params );
}
}
return $grouped;
}