Help in php code

Discussion in 'PHP' started by Alex20m, Nov 25, 2007.

  1. #1
    Hello all,

    I a tree, and my code should move the children of one node to another one. My code doesn't give a correct result, I think I implemented it wrongly, can u help me?...

    <?php
    $root_node=array("parent"=>null, "child"=>"root");
    $a= array("parent"=>"root", "child"=>"a");
    $tree=array($root_node,$a);
    $tree[]=array("parent"=>"root","child"=>"b");
    ?>

    <?php
    $t[]=array(null,"root");
    $t[]=array("root","a");
    $t[]=array("root","b");
    $t[]=array("root","c");
    $t[]=array("a","d");
    $t[]=array("a","e");
    $t[]=array("a","f");
    $t[]=array("e","x");
    $t[]=array("e","y");
    $t[]=array("f","z");


    ?>

    <?php
    function find_children($tree,$parent){
    foreach($tree as $node){
    if($node[0]==$parent[1]){
    $children[]=$node;
    }
    }
    return $children;
    }
    //-------------------------------------
    $node_a=array("root","a");
    $children=find_children($t,$node_a);

    ?>

    <?php
    function move_subtree(&$tree,$old,$new){
    $childr[]=find_children($tree,$old);
    while($childr[0]!=null){
    foreach($childr as $i=>$ch){
    unset($old);
    $t[]=array($new,$ch);
    }
    }
    return $t;
    }
    $node_x=array("root","a");

    $node_c=array("root","c");

    $t2[]=move_subtree($t,$node_x,$node_c);
    print_r($t2);

    ?>
     
    Alex20m, Nov 25, 2007 IP