public function toHierarchy($collection): array
    {
        // Trees mapped
        $trees = [];
        if (count($collection) > 0) {
            // Node Stack. Used to help building the hierarchy
            $stack = [];
            foreach ($collection as $item) {
                $item[$this->treeKey] = [];
                // Number of stack items
                $l = count($stack);
                // Check if we're dealing with different levels
                while ($l > 0 && $stack[$l - 1]['level'] >= $item['level']) {
                    array_pop($stack);
                    --$l;
                }
                // Stack is empty (we are inspecting the root)
                if (0 == $l) {
                    // Assigning the root node
                    $i = count($trees);
                    $trees[$i] = $item;
                    $stack[] = &$trees[$i];
                } else {
                    // Add node to parent
                    $i = count($stack[$l - 1][$this->treeKey]);
                    $stack[$l - 1][$this->treeKey][$i] = $item;
                    $stack[] = &$stack[$l - 1][$this->treeKey][$i];
                }
            }
        }

        return $trees;
    }
public function toHierarchy($collection): array
    {
        // Trees mapped
        $trees = [];
        if (count($collection) > 0) {
            // Node Stack. Used to help building the hierarchy
            $stack = [];
            foreach ($collection as $item) {
                $item[$this->treeKey] = [];
                // Number of stack items
                $l = count($stack);
                // Check if we're dealing with different levels
                while ($l > 0 && $stack[$l - 1]['level'] >= $item['level']) {
                    array_pop($stack);
                    --$l;
                }
                // Stack is empty (we are inspecting the root)
                if (0 == $l) {
                    // Assigning the root node
                    $i = count($trees);
                    $trees[$i] = $item;
                    $stack[] = &$trees[$i];
                } else {
                    // Add node to parent
                    $i = count($stack[$l - 1][$this->treeKey]);
                    $stack[$l - 1][$this->treeKey][$i] = $item;
                    $stack[] = &$stack[$l - 1][$this->treeKey][$i];
                }
            }
        }

        return $trees;
    }
public function toHierarchy ( $ collection ) : array { // Trees mapped $ trees = [ ] ; if ( count ( $ collection ) > 0 ) { // Node Stack. Used to help building the hierarchy $ stack = [ ] ; foreach ( $ collection as $ item ) { $ item [ $ this -> treeKey ] = [ ] ; // Number of stack items $ l = count ( $ stack ) ; // Check if we're dealing with different levels while ( $ l > 0 && $ stack [ $ l - 1 ] [ 'level' ] >= $ item [ 'level' ] ) { array_pop ( $ stack ) ; -- $ l ; } // Stack is empty (we are inspecting the root) if ( 0 == $ l ) { // Assigning the root node $ i = count ( $ trees ) ; $ trees [ $ i ] = $ item ; $ stack [ ] = & $ trees [ $ i ] ; } else { // Add node to parent $ i = count ( $ stack [ $ l - 1 ] [ $ this -> treeKey ] ) ; $ stack [ $ l - 1 ] [ $ this -> treeKey ] [ $ i ] = $ item ; $ stack [ ] = & $ stack [ $ l - 1 ] [ $ this -> treeKey ] [ $ i ] ; } } } return $ trees ; }
Make hierarchy array by level.

@param $collection Model[]

@return array
Make hierarchy array by level .