src/Association.php
public static function groupResult(array $original, array $keys, $level = 0)
    {
        $converted = [];
        $key = $keys[$level];
        $isDeepest = sizeof($keys) - 1 == $level;

        $level++;

        $filtered = [];
        foreach ($original as $k => $subArray) {

            $subArray = (array) $subArray;
            if (!isset($subArray[$key])) {
                throw new AssociationException(
                    "Index '" . $key . "' not found on level '" . $level . "'!"
                );
            }

            $thisLevel = $subArray[$key];

            if (is_object($thisLevel)) {
                $thisLevel = (string) $thisLevel;
            }

            if ($isDeepest) {
                $converted[$thisLevel] = $subArray;
            } else {
                $converted[$thisLevel] = [];
            }
            $filtered[$thisLevel][] = $subArray;
        }

        if (!$isDeepest) {
            foreach (array_keys($converted) as $value) {
                $converted[$value] = self::groupResult(
                    $filtered[$value],
                    $keys,
                    $level
                );
            }
        }

        return $converted;
    }
public static function groupResult(array $original, array $keys, $level = 0)
    {
        $converted = [];
        $key = $keys[$level];
        $isDeepest = sizeof($keys) - 1 == $level;

        $level++;

        $filtered = [];
        foreach ($original as $k => $subArray) {

            $subArray = (array) $subArray;
            if (!isset($subArray[$key])) {
                throw new AssociationException(
                    "Index '" . $key . "' not found on level '" . $level . "'!"
                );
            }

            $thisLevel = $subArray[$key];

            if (is_object($thisLevel)) {
                $thisLevel = (string) $thisLevel;
            }

            if ($isDeepest) {
                $converted[$thisLevel] = $subArray;
            } else {
                $converted[$thisLevel] = [];
            }
            $filtered[$thisLevel][] = $subArray;
        }

        if (!$isDeepest) {
            foreach (array_keys($converted) as $value) {
                $converted[$value] = self::groupResult(
                    $filtered[$value],
                    $keys,
                    $level
                );
            }
        }

        return $converted;
    }
public static function groupResult ( array $ original , array $ keys , $ level = 0 ) { $ converted = [ ] ; $ key = $ keys [ $ level ] ; $ isDeepest = sizeof ( $ keys ) - 1 == $ level ; $ level ++ ; $ filtered = [ ] ; foreach ( $ original as $ k => $ subArray ) { $ subArray = ( array ) $ subArray ; if ( ! isset ( $ subArray [ $ key ] ) ) { throw new AssociationException ( "Index '" . $ key . "' not found on level '" . $ level . "'!" ) ; } $ thisLevel = $ subArray [ $ key ] ; if ( is_object ( $ thisLevel ) ) { $ thisLevel = ( string ) $ thisLevel ; } if ( $ isDeepest ) { $ converted [ $ thisLevel ] = $ subArray ; } else { $ converted [ $ thisLevel ] = [ ] ; } $ filtered [ $ thisLevel ] [ ] = $ subArray ; } if ( ! $ isDeepest ) { foreach ( array_keys ( $ converted ) as $ value ) { $ converted [ $ value ] = self :: groupResult ( $ filtered [ $ value ] , $ keys , $ level ) ; } } return $ converted ; }
Group associative array

@param array $original
@param array $keys
@param int   $level

@return array

@link http://tigrou.nl/2012/11/26/group-a-php-array-to-a-tree-structure/

@throws \Exception
Group associative array