建议楼主看看C的递归程序,跟php差不多。

解决方案 »

  1.   

    In a function, there are some lines calling itself, this is recursion, notice you must have some code to end the calling to itself, otherwise, calling stack will overflow.
      

  2.   

    For example:A recursion sample writing in C#.
            private void recursionTree(TreeNode treeNode, XmlNode parentNode)
            {
                foreach (XmlNode node in parentNode)
                {
                    TreeNode tmp;                if (node.HasChildNodes)
                        tmp = new TreeNode(parentNode.Name);
                    else
                    {
                        tmp = new TreeNode(parentNode.InnerText);                    this.treeToXmlMapping.Add(tmp, parentNode);
                    }                treeNode.Nodes.Add(tmp);                   if (node.HasChildNodes)
                            this.recursionTree(tmp, node);
                }
            }
      

  3.   

    今天刚写的一个小东东,给你吧,一起研究
    <?php
    /*
    //  对给出的数组进行排列
    //  数据结构:数组,递归,循环
    //------------------------------------------------
    //    arrange()
    // copyright (c) 2004
    // xmlv
    //
    //--------------------------------------------------
    */
    function pailie($myarr){
    $exist_arr=$myarr;
    $left_num=$GLOBALS['total_num']-count($exist_arr);
    $left_array=array_diff($GLOBALS['total_arr'],$exist_arr);
             //测试是否已经到达末尾
    if ($left_num!=0){
    foreach ($left_array as $value){
    #生成新的树租保存即将传到下一级的新数租
    $new_exist_arr=$exist_arr;
    array_push($new_exist_arr,$value);
    pailie($new_exist_arr);
    }
    }
    else{

    foreach ($exist_arr as $value){
    echo $value.",";
    }
    echo "<br>";

    }
    }
    function arrange(){
    global $total_arr;
    global $total_num;
    $total_arr=array("a","b","c");
    $total_num=count($total_arr);
    $exist_arr=array();
    pailie($exist_arr);}
    arrange();
    ?>
      

  4.   

    //1+.........100
    echo add(100);
    function add($i){
       $i= $i==1? 1 :$i+add($i-1);
       return $i;
    }
      

  5.   

    用递归和循环不是差不多嘛。比如楼上的这个add()例子。