递归?汗一把,试过好几次写递归都失败了。不过把前一次结果带进来,估计是unset(变量)没设或位置没对吧。

解决方案 »

  1.   

    能把问题说清楚一点吗????
    你现在从某个页面掏一个函数出来,id,又终极id??
    你说的表存储着什么? 字段如何??? 你都没有说清楚.
    让人看起来一头雾水
      

  2.   

    这就是一个无限级分类的应用,根据任何一个id,取得下面最终分类的id数组.
      

  3.   

    不知道是否楼主所想要的
    function get_child_idarr($id, $isFrist=false)
    {
    global $db_prefix,$sql;
    static $str=array();
             if ($isFrist) empty($str);
    $a=array();
    $rs=$sql->query("SELECT id,rou_id FROM ".$db_prefix."prot_class WHERE `p_id`='$id'");
    if($sql->num_rows($rs) > 0)
    {
    while($r=$sql->fetch_assoc($rs))
    {
    $p_id_num=explode(":",$r['rou_id']);
    if(count($p_id_num)==3 AND 1 > $sql->num_rows($sql->query("SELECT id FROM ".$db_prefix."prot_class WHERE `p_id`=$r[id]")))
    {
    if(array_search($r['id'],$str)===null)
    {
    $str[]=$r['id'];
    }
    }
    get_child_idarr($r['id']);
    }
    }else
    {
    $str[]=$id;
    }
    $a=$str;
    unset($str);
    return $a;
    }
      

  4.   

    函数中有
    static $str=array();

    unset($str);但是请注意:静态变量是不会被unset删除的,也就是说一旦声明了静态变量,那么他将一直存在直到程序结束
    你只需把
    unset($str);
    改成
    $str=array();
    就可以了