1.将arr数组下的所有$id的下级全部找到并放入数组childs中
public function child(&$arr,$id)
{
$childs=array();
foreach ($arr as $k => $v)
{
if ($v['uid'] == $id)
{
$childs[]=$v;
}
}
return $childs;
}
2.通过递归的方式找到所有的下级
public function tree($id) { $data=Db::name('xinxi')->select(); $childs=$this->child($data,$id); if (empty($childs)) { return null; } foreach ($childs as $k =>$v) { $res=$this->tree($v['id']); if (null != $res) { $childs[$k]['childs']=$res; } } return $childs; }
3.再次通过递归的方式,取得所有下级中的某些值
public function all($trees)
{
$all=0;
foreach ($trees as $tree)
{
if ($tree['uid'] > 0)
{
$all=$all+$tree['hyd'];
}
if (isset($tree['childs'])){
$all=$all+$this->all($tree['childs']);
}
}
return $all;
}
0条评论