/** * 递归权限(数组) * * @param $data select查询来未处理过的数组 * @param $pid * @return array */function recursiveMenuArray($data,$pid){ $re_data =[]; foreach ($data as $key=>$value){ if($value['pid'] == $pid){ $re_data[$value['id']] = $value; $re_data[$value['id']]['list'] = $this->recursionMenu($data,$value['id']); }else{ continue; } } return $re_data;}$data = query("select * from menu");//二维数组$array = recursiveMenuArray($data,0);print_r($array);
输出结果:Array( [1] => Array ( [id] => 1 [title] => 操作员列表 [pid] => 0 [list] => Array ( [2] => Array ( [id] => 2 [title] => 在线配置 [pid] => 1 [list] => Array ( [3] => Array ( [id] => 3 [title] => 服务器信息 [pid] => 2 [list] => Array ( [4] => Array ( [id] => 4 [title] => 游戏账号 [pid] => 3 [list] => Array ( ) ) [5] => Array ( [id] => 5 [title] => 查询 [pid] => 3 [list] => Array ( ) ) [6] => Array ( [id] => 6 [title] => 邮件及兑换码 [pid] => 3 [list] => Array ( ) ) ) ) ) ) ) ))