public static function groupBy($array, $groupBy = "id")
	{
		return static::reduce($array, function($arr, $item) use($groupBy) {

			// get value of attribute
			if( is_closure($groupBy) )
			{
				// execute closure
				$value = $groupBy($item);
			} 
			else
			{
				// get attribute of object
				$value = pisc\upperscore\def($item, $groupBy);
			}

			// build new array with value as keys
			if( isset($arr[$value]) )
			{
				$arr[$value][] = $item;
			}
			else
			{
				$arr[$value] = [ $item ];
			}

			return $arr;

		}, []);
	}
public static function groupBy($array, $groupBy = "id")
	{
		return static::reduce($array, function($arr, $item) use($groupBy) {

			// get value of attribute
			if( is_closure($groupBy) )
			{
				// execute closure
				$value = $groupBy($item);
			} 
			else
			{
				// get attribute of object
				$value = pisc\upperscore\def($item, $groupBy);
			}

			// build new array with value as keys
			if( isset($arr[$value]) )
			{
				$arr[$value][] = $item;
			}
			else
			{
				$arr[$value] = [ $item ];
			}

			return $arr;

		}, []);
	}
public static function groupBy ( $ array , $ groupBy = "id" ) { return static :: reduce ( $ array , function ( $ arr , $ item ) use ( $ groupBy ) { // get value of attribute if ( is_closure ( $ groupBy ) ) { // execute closure $ value = $ groupBy ( $ item ) ; } else { // get attribute of object $ value = pisc \ upperscore \ def ( $ item , $ groupBy ) ; } // build new array with value as keys if ( isset ( $ arr [ $ value ] ) ) { $ arr [ $ value ] [ ] = $ item ; } else { $ arr [ $ value ] = [ $ item ] ; } return $ arr ; } , [ ] ) ; }
Group an array of objects by an attribute of an object or the result of a closure

@param  array           $array    Array of objects
@param  string/closure  $groupBy  Attribute of object or a closure. Closure gets object as parameter and should return a value that can be a key of the array
@return array                     Array of arrays
Group an array of objects by an attribute of an object or the result of a closure