如何用递归取得某个id的所有子分类
时间:2006-01-16 来源:wleige
1,'pid'=>0),
array('id'=>2,'pid'=>1),
array('id'=>3,'pid'=>1),
array('id'=>4,'pid'=>3),
array('id'=>5,'pid'=>2),
array('id'=>6,'pid'=>2),
);
function getChilds($arr,$pid,$deep=0)
{
static $childs;
$deep++;
if(isset($arr) && !empty($arr))
{
foreach($arr as $val)
{
if($val["pid"] == $pid)
{
$i = isset($childs)?count($childs):0;
$childs[$i]["id"] = $val["id"];
$childs[$i]["pid"] = $pid;
$childs[$i]["deep"] = $deep-1;
getChilds($arr,$val["id"],$deep);
$i++;
}
}
return $childs;
}
else
return false;
}
$data = getChilds($data,1);
print_r($data);
?>
array('id'=>2,'pid'=>1),
array('id'=>3,'pid'=>1),
array('id'=>4,'pid'=>3),
array('id'=>5,'pid'=>2),
array('id'=>6,'pid'=>2),
);
function getChilds($arr,$pid,$deep=0)
{
static $childs;
$deep++;
if(isset($arr) && !empty($arr))
{
foreach($arr as $val)
{
if($val["pid"] == $pid)
{
$i = isset($childs)?count($childs):0;
$childs[$i]["id"] = $val["id"];
$childs[$i]["pid"] = $pid;
$childs[$i]["deep"] = $deep-1;
getChilds($arr,$val["id"],$deep);
$i++;
}
}
return $childs;
}
else
return false;
}
$data = getChilds($data,1);
print_r($data);
?>
相关阅读 更多 +