protected function toTree(array $dataSet = [])
    {
        $tree = [];

        /*
         * Most dataSets in the wild are enumerative arrays and we need associative array
         * where the same ID used for addressing parents is used. We make associative
         * array on the fly
        */
        $references = [];

        foreach ($dataSet as $id => &$node) {
            // Add the node to our associative array using it's ID as key
            $references[$node['id']] = &$node;

            // Add empty placeholder for children
            $node['children'] = [];

            // It it's a root node, we add it directly to the tree
            if (is_null($node['parent_id'])) {
                $tree[$node['id']] = &$node;
            } else {
                // It was not a root node, add this node as a reference in the parent.
                $references[$node['parent_id']]['children'][$node['id']] = &$node;
            }
        }

        return $tree;
    }
protected function toTree(array $dataSet = [])
    {
        $tree = [];

        /*
         * Most dataSets in the wild are enumerative arrays and we need associative array
         * where the same ID used for addressing parents is used. We make associative
         * array on the fly
        */
        $references = [];

        foreach ($dataSet as $id => &$node) {
            // Add the node to our associative array using it's ID as key
            $references[$node['id']] = &$node;

            // Add empty placeholder for children
            $node['children'] = [];

            // It it's a root node, we add it directly to the tree
            if (is_null($node['parent_id'])) {
                $tree[$node['id']] = &$node;
            } else {
                // It was not a root node, add this node as a reference in the parent.
                $references[$node['parent_id']]['children'][$node['id']] = &$node;
            }
        }

        return $tree;
    }
protected function toTree ( array $ dataSet = [ ] ) { $ tree = [ ] ; /*
         * Most dataSets in the wild are enumerative arrays and we need associative array
         * where the same ID used for addressing parents is used. We make associative
         * array on the fly
        */ $ references = [ ] ; foreach ( $ dataSet as $ id => & $ node ) { // Add the node to our associative array using it's ID as key $ references [ $ node [ 'id' ] ] = & $ node ; // Add empty placeholder for children $ node [ 'children' ] = [ ] ; // It it's a root node, we add it directly to the tree if ( is_null ( $ node [ 'parent_id' ] ) ) { $ tree [ $ node [ 'id' ] ] = & $ node ; } else { // It was not a root node, add this node as a reference in the parent. $ references [ $ node [ 'parent_id' ] ] [ 'children' ] [ $ node [ 'id' ] ] = & $ node ; } } return $ tree ; }
Transform dataSet to a tree

@param array $dataSet
@return array
Transform dataSet to a tree