[code] $aa = Db::table(\"grade_agent\")->where(\"bid\",\"in\",implode(\',\',$user_team_id))->field(\"bid,tid\")->select();$res=sotr($aa,$user[\"id\"]);function sotr($data,$pid=0,$level=0){$list =array();foreach ($data as $k=>$v){if ($v[\'tid\'] == $pid){$v[\'level\'] = $level;$v[\'son\'] =sotr($data,$v[\'bid\'],$level+1);$list[] = $v;}}return $list;}
打印结果
[code]Array([0] => Array([bid] => 147[tid] => 146[level] => 0[son] => Array())[1] => Array([bid] => 148[tid] => 146[level] => 0[son] => Array([0] => Array([bid] => 149[tid] => 148[level] => 1[son] => Array([0] => Array([bid] => 150[tid] => 149[level] => 2[son] => Array([0] => Array([bid] => 153[tid] => 150[level] => 3[son] => Array())))))[1] => Array([bid] => 154[tid] => 148[level] => 1[son] => Array([0] => Array([bid] => 155[tid] => 154[level] => 2[son] => Array())))[2] => Array([bid] => 156[tid] => 148[level] => 1[son] => Array())))[2] => Array([bid] => 151[tid] => 146[level] => 0[son] => Array())[3] => Array([bid] => 152[tid] => 146[level] => 0[son] => Array()))