~~~
/**
* 用递归获根据id获取所有的上级
* @param $cate
* @param $id
* @return array
*/
function get_top_pid($cate,$id){
$arr=array();
foreach($cate as $v){
if($v['id']==$id){
$arr[]=$v;// $arr[$v['id']]=$v['name'];
$arr=array_merge(get_top_pid($cate,$v['agent_id']),$arr);
}
}
return $arr;
}
~~~
~~~
/**
* 调用方法如下
* 获取顶级总代理
*/
public function merchants_agent_id($id){
$merchants_users = M("merchants_users")->field("id,agent_id")->select();
if (empty($id)){
$this->error("商户ID不能为空");
}else{
$data = get_top_pid($merchants_users,$id);
foreach ($data as &$v){
if ($v['agent_id'] == 0){
$agent_top_name = M("merchants_agent")->where(array("uid"=>$v['id']))->getField("agent_name");
}
}
return $agent_top_name;
}
}
~~~
~~~