Data Structure in PHP

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

  1. #1
    Hello All,

    I am trying to solve a question about Trees in PHP, I hope any one can help.

    The question is moving children from one parent of the tree to another one. I have an error in my code telling me (Warning: Invalid argument supplied for foreach() in C:\Program Files\nusphere\phped\Projects\trial.php5 on line 18)....
    I think I implemented the code wrongly, any suggestions to solve it?...I will be really thankful.


    <?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("c","g");
    $t[]=array("c","h");
    $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(&$tree,$old,$new){
    $old=$parent;
    $child[]=find_children($tree,$old);
    while($child[0]!=null){
    foreach($child as $i=>$ch){
    $t2=array_push($new,$children);
    }
    }
    return $t2;
    }
    $node_x=array("root","a");

    $node_c=array("root","c");
    $node_g=array("c","g");
    $t=move(&$tree,$node_x,$node_c);
    print_r($t);
    ?>
     
    Alex20m, Nov 25, 2007 IP