/** * 获取分类的子id列表 * @param int $id 传入的id * @return string */ ~~~ function getChild($id){ $data= Db::name("category")->field("id,pid")->select(); $arr = array( ); $temp = array(); foreach ( $data as $key => $v ) { //判断,如果$v['pid'] == $pid的则压入数组Child if ($v['pid'] == $id) { //递归执行 $temp[] = $v['id']; $temp = array_merge($temp,getChild($v['id'],$data)); } } return $temp; } ~~~ / * 获取分类的父id列表 * @param int $id 传入的id * @return string / ~~~ function getParent($id = 0){ $data= Db::name("category")->field("id,pid")->select(); $arr = array( ); $temp = array(); foreach ( $data as $key => $v ) { //判断,如果$v['pid'] == $pid的则压入数组Child if ($v['id'] == $id&&$v['pid']!=0) { //递归执行 $temp[] = $v['pid']; $temp = array_merge($temp,getParent($v['pid'],$data)); } } return $temp; } ~~~