//更新数据 public function update(){ $wx = M('wxmenu'); $map['pid'] = 0; //查询主菜单数据 $wxmenu = $wx->where($map)->select(); $arr = array(); //循环主菜单 foreach ($wxmenu as $key => $value) { $parent_arr = array(); $map['pid'] = $value['id']; $submenu = $wx->where($map)->select(); //判断是否有子菜单(默认有) $has_sub = true; //如果没有子菜单 if(empty($submenu)){ $parent_arr['type'] = $value['type']; $has_sub = false; } $parent_arr['name'] = $value['name']; //控制显示顺序 if(!$has_sub){ $parent_arr[$value['type'] == 'click' ? 'key' : 'url'] = $value['url']; } $subdata = array(); //循环子菜单 foreach ($submenu as $k=>$v) { $tmp_ku = $v['type'] == 'click' ? 'key' : 'url'; $tmp_arr = array( 'type' => $v['type'], 'name' => $v['name'], $tmp_ku => $v['url'] ); array_push($subdata,$tmp_arr); } if($has_sub){ $parent_arr['sub_button'] = $subdata; } array_push($arr,$parent_arr); } dump($arr); dump($arr[0]); //转化成json结构 echo json_encode(array('button'=>$arr)); die(); }