1.
function arrayToTreeArray($rows)
{
    if(!is_array($rows) || empty($rows) ){
        return false;
    }
    // $rows = array();  //stores all the database rows that are to be converted into a tree
    $tree = array();  //stores the tree
    $tree_index = array();  //an array used to quickly find nodes in the tree
    $id_column = "idx";  //The column that contains the id of each node
    $parent_column = "pidx";  //The column that contains the id of each node's parent
    $text_column = "title";  //The column to display when printing the tree to html
    //build the tree - this will complete in a single pass if no parents are defined after children
    // vp(count($rows) );die();
    // while(count($rows) > 0){
    foreach($rows as $row_id => $row){
        $row_id = $row[$id_column];
        if($row[$parent_column]){
            if((!array_key_exists($row[$parent_column], $rows)) and (!array_key_exists($row[$parent_column], $tree_index))){
               unset($rows[$row_id]);
            }
            else{
              if(array_key_exists($row[$parent_column], $tree_index)){
                $parent = & $tree_index[$row[$parent_column]];
                $parent['children'][$row_id] =$row;
                $parent['children'][$row_id]["children"] = array();
                $tree_index[$row_id] = & $parent['children'][$row_id];
                unset($rows[$row_id]);
              }
            }
        }
        else{
            $tree[$row_id] = $row;
            $tree[$row_id]["children"] = array();
            $tree_index[$row_id] = & $tree[$row_id];
            unset($rows[$row_id]);
        }
    }
    // }
    return $tree;
}



2.
$tree = array (
    obj(parentseq:0, menuseq:1, name:'aaa', subMenu:obj(array) ),
    obj(parentseq:1, menuseq:2, name:'aaa', subMenu:obj(array) ),
    obj(parentseq:1, menuseq:3, name:'aaa', subMenu:obj(array) ),
    obj(parentseq:3, menuseq:4, name:'aaa', subMenu:obj(array) ),
);

$menu = array();
        $ref = array();
        foreach( $tree as $k => $d ) {
            //$d['children'] = array();
            if( isset( $ref[ $d->getParentMenuSeq() ] ) ) { // we have a reference on its parent
                $ref[ $d->getParentMenuSeq() ]->setGNBSubMenu($d,$d->getGNBMenuSeq());
                $ref[ $d->getGNBMenuSeq() ] =& $ref[ $d->getParentMenuSeq() ]->getGNBSubMenu($d->getGNBMenuSeq());
            } else { // we don't have a reference on its parent => put it a root level
                $menu[ $d->getGNBMenuSeq() ] = $d;
                $ref[ $d->getGNBMenuSeq() ] =& $menu[ $d->getGNBMenuSeq() ];
            }
        }
       
        /*
        $tree = array (
            Array ('CategoryID' => 1, 'ParentID' => 0, 'Depth' => 1),
            Array ('CategoryID' => 2, 'ParentID' => 1, 'Depth' => 2),
            Array ('CategoryID' => 3, 'ParentID' => 2, 'Depth' => 2),
            Array ('CategoryID' => 4, 'ParentID' => 0, 'Depth' => 1),
            Array ('CategoryID' => 5, 'ParentID' => 2, 'Depth' => 2),
            Array ('CategoryID' => 6, 'ParentID' => 3, 'Depth' => 3),
            Array ('CategoryID' => 7, 'ParentID' => 1, 'Depth' => 2),
        );
        $menu = array();
        $ref = array();
        foreach( $tree as $d ) {
            $d['children'] = array();
            if( isset( $ref[ $d['ParentID'] ] ) ) { // we have a reference on its parent
                $ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ] = $d;
                $ref[ $d['CategoryID'] ] =& $ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ];
            } else { // we don't have a reference on its parent => put it a root level
                $menu[ $d['CategoryID'] ] = $d;
                $ref[ $d['CategoryID'] ] =& $menu[ $d['CategoryID'] ];
            }
        }
         */
        /*
        $tree = array (
            Array ('CategoryID' => 1, 'ParentID' => 0, 'Depth' => 1, 'Sub'=>Array()),
            Array ('CategoryID' => 2, 'ParentID' => 1, 'Depth' => 2, 'Sub'=>Array()),
            Array ('CategoryID' => 3, 'ParentID' => 2, 'Depth' => 2, 'Sub'=>Array()),
            Array ('CategoryID' => 4, 'ParentID' => 0, 'Depth' => 1, 'Sub'=>Array()),
            Array ('CategoryID' => 5, 'ParentID' => 2, 'Depth' => 2, 'Sub'=>Array()),
            Array ('CategoryID' => 6, 'ParentID' => 3, 'Depth' => 3, 'Sub'=>Array()),
            Array ('CategoryID' => 7, 'ParentID' => 1, 'Depth' => 2, 'Sub'=>Array()),
        );
        $menu = array();
        $ref = array();
        foreach( $tree as $k => $d ) {
            //$d['children'] = array();
            if( isset( $ref[ $d['ParentID'] ] ) ) { // we have a reference on its parent
                $ref[ $d['ParentID'] ]['Sub'][ $d['CategoryID'] ] = $d;
                $ref[ $d['CategoryID'] ] =& $ref[ $d['ParentID'] ]['Sub'][ $d['CategoryID'] ];
            } else { // we don't have a reference on its parent => put it a root level
                $menu[ $d['CategoryID'] ] = $d;
                $ref[ $d['CategoryID'] ] =& $menu[ $d['CategoryID'] ];
            }
        }
         */


3.
function array_stack (&$a, $p = '@parent', $c = '@children')
{
  $l = $t = array();
  foreach ($a AS $key => $val):
    if (!$val[$p]) $t[$key] =& $l[$key];
    else $l[$val[$p]][$c][$key] =& $l[$key];
    $l[$key] = (array)$l[$key] + $val;
  endforeach;
  return $a = array('tree' => $t, 'leaf' => $l);
}

$node = array();
$node[1] = array('@parent' => 0, 'title' => 'I am node 1.');
#     ^-----------------------v Link @parent value to key.
$node[2] = array('@parent' => 1, 'title' => 'I am node 2.');
$node[3] = array('@parent' => 2, 'title' => 'I am node 3.');
$node[4] = array('@parent' => 1, 'title' => 'I am node 4.');
$node[5] = array('@parent' => 4, 'title' => 'I am node 5.');

echo '<pre>';
print_r ( array_stack($node) );

+ Recent posts