PHP recursive get unlimited category menu

Source: Internet
Author: User
Tags all mail

To get all the menu information from the database, you need to get the main menu and its submenus according to the Id,pid field, as well as submenus under the submenu, which can be implemented by function recursion.

<?PHPclassMenu { Public $menu=Array();  Public function__construct () {$this->menu =Array(            Array(' id ' =>1, ' title ' = ' "Home", ' pid ' =>0),Array(' id ' =>158, ' title ' = ' "basic Information", ' pid ' =>157),Array(' id ' =>159, ' title ' = ' "Production and elimination Log", ' pid ' =>157),Array(' id ' =>147, ' title ' = ' Player management ', ' pid ' =>124),Array(' id ' =>148, ' title ' = ' = ' Kick-line-stop-ban ', ' pid ' =>147),Array(' id ' =>149, ' title ' = ' Game announcement ', ' pid ' =>147),Array(' id ' =>150, ' title ' = ' "gift card", ' pid ' =>147),Array(' id ' =>151, ' title ' = ' = ' mail sent ', ' pid ' =>147),Array(' id ' =>152, ' title ' = ' = ' all mail sent ', ' pid ' =>147),Array(' id ' =>160, ' title ' = ' Player adjust ', ' pid ' =>147),Array(' id ' =>154, ' title ' = ' "Approval management", ' pid ' =>147),Array(' id ' =>155, ' title ' = ' "GM directive", ' pid ' =>147),Array(' id ' =>156, ' title ' = ' + ' server parameter configuration ', ' pid ' =>147),Array(' id ' =>157, ' title ' = ' "Log query", ' pid ' =>124),Array(' id ' =>123, ' title ' = ' data query ', ' pid ' =>0),Array(' id ' =>124, ' title ' = ' Game Management ', ' pid ' =>0),Array(' id ' =>126, ' title ' = ' "system tool", ' pid ' = 0),Array(' id ' =>127, ' title ' = ' Rights Management ', ' pid ' = 0),Array(' id ' =>128, ' title ' = ' Data overview ', ' pid ' =>123),Array(' id ' =>129, ' title ' = ' = ' Login to build the corner with online data ', ' pid ' =>123),Array(' id ' =>130, ' title ' = ' retained data analysis ', ' pid ' =>123),Array(' id ' =>131, ' title ' = ' "Pay statistics", ' pid ' =>123),Array(' id ' =>132, ' title ' = ' Consumption data analysis ', ' pid ' =>123),Array(' id ' =>134, ' title ' = ' Real time Stats ', ' pid ' =>129),Array(' id ' =>135, ' title ' = ' Historical Statistics ', ' pid ' =>129),Array(' id ' =>137, ' title ' = ' "Day retained", ' pid ' =>130),Array(' id ' =>138, ' title ' = ' "Level & Task Retention", ' pid ' =>130),Array(' id ' =>139, ' title ' and ' = ' "sub-career statistics", ' pid ' =>130),Array(' id ' =>140, ' title ' = ' + ' Daily recharge statistics ', ' pid ' =>131),Array(' id ' =>141, ' title ' = ' + ' "recharge order", ' pid ' = 131),Array(' id ' =>142, ' title ' = ' Top up ', ' pid ' =>131),Array(' id ' =>143, ' title ' = ' Money consumption ', ' pid ' =>132),Array(' id ' =>144, ' title ' = ' "Currency day retention", ' pid ' =>132),Array(' id ' =>145, ' title ' = ' User management ', ' pid ' =>127),Array(' id ' =>146, ' title ' = ' User group management ', ' PID ' =>127)        ); return $this-GetMenu (); }    /** * Main Menu pid is 0 * @return Array*/         protected functionGetMenu () {foreach($this->menus as $key=$items) {            if($items[' pid '] = = "0" ) {                unset($this->menu[$key]); $menu[] =$this->buildmenutree ($items,$items[' ID ']); }        }        return $menu; }    /** * Production of multi-level menu tree * @param array $items * @param int $rid * @return Array*/    protected functionBuildmenutree ($items,$rid) {        $childs=$this->getchildmenu ($items,$rid); if(isset($childs[' Child '])) {            foreach($childs[' Child '] as $key=$value) {                $children=$this->buildmenutree ($value,$value[' ID ']); if(NULL!=$children[' Child ']) {                    $childs[' Child '] [$key[' child '] =$children[' Child ']; }            }        }        return $childs; }        /** * Get sub-menu **/    protected functionGetchildmenu ($items,$rid) {        foreach($this->menu as $key=$value) {            if($value[' pid '] = =$rid) {                unset($this->menu[$key]); $items[' Child '] [] =$value; }        }        return $items; }    }Var_dump(NewMenu);


The test results are:
Array (5) {[0] = = Array (4{["id"] + = string (1) "1"["title"] +-string (6) "Home"["pid"] = + string (1) "0"["sort"] = + string (1) "1"} [1] = = Array (5{["id"] + = string (3) "123"["title"] + + string (12) "Data query"["pid"] = + string (1) "0"["sort"] = + string (1) "1"["child"] = = Array (5) {[0] = = Array (4{["id"] + = string (3) "128"["title"] + + string (12) "Data Overview"["pid"] = + string (3) "123"["sort"] = + string (1) "0"} [1] = = Array (5{["id"] + = string (3) "129"["title"] + + string (27) "Login Build and online data"["pid"] = + string (3) "123"["sort"] = + string (1) "0"["child"] = = Array (2) {[0] = = Array (4{["id"] + = string (3) "134"["title"] and string (12) "Real Time Statistics"["pid"] = + string (3) "129"["sort"] = + string (1) "0"} [1] = = Array (4{["id"] + = string (3) "135"["title"] + = string (12) "Historical statistics"["pid"] = + string (3) "129"["sort"] = + string (1) "0"}}} [2] = = Array (5{["id"] + = string (3) "130"["title"] + string (18) "Retained data analysis"["pid"] = + string (3) "123"["sort"] = + string (1) "0"["child"] = = Array (3) {[0] = = Array (4{["id"] + = string (3) "137"["title"] + string (9) "Day of Preservation"["pid"] = + string (3) "130"["sort"] = + string (1) "0"} [1] = = Array (4{["id"] + = string (3) "138"["title"] + string (19) "Level & Task Retention"["pid"] = + string (3) "130"["sort"] = + string (1) "0"} [2] = = Array (4{["id"] + = string (3) "139"["title"] + string (15) "Occupation statistics"["pid"] = + string (3) "130"["sort"] = + string (1) "0"}}} [3] = = Array (5{["id"] + = string (3) "131"["title"] + + string (12) "Paid statistics"["pid"] = + string (3) "123"["sort"] = + string (1) "0"["child"] = = Array (3) {[0] = = Array (4{["id"] + = string (3) "140"["title"] + + string (15) "Daily Recharge Statistics"["pid"] = + string (3) "131"["sort"] = + string (1) "0"} [1] = = Array (4{["id"] + = string (3) "141"["title"] + + string (12) "Recharge order"["pid"] = + string (3) "131"["sort"] = + string (1) "0"} [2] = = Array (4{["id"] + = string (3) "142"["title"] + string (12) "Top ranking"["pid"] = + string (3) "131"["sort"] = + string (1) "0"}}} [4] = = Array (5{["id"] + = string (3) "132"["title"] and string (18) "Consumption data Analysis"["pid"] = + string (3) "123"["sort"] = + string (1) "0"["child"] = = Array (2) {[0] = = Array (4{["id"] + = string (3) "143"["title"] + = string (12) "Currency consumption"["pid"] = + string (3) "132"["sort"] = + string (1) "0"} [1] = = Array (4{["id"] + = string (3) "144"["title"] and string (15) "Currency Day retained"["pid"] = + string (3) "132"["sort"] = + string (1) "0"}}}}} [2] = = Array (5{["id"] + = string (3) "124"["title"] and string (12) "Game management"["pid"] = + string (1) "0"["sort"] = + string (1) "3"["child"] = = Array (2) {[0] = = Array (5{["id"] + = string (3) "147"["title"] and string (12) "Player management"["pid"] = + string (3) "124"["sort"] = + string (1) "0"["child"] = = Array (9) {[0] = = Array (4{["id"] + = string (3) "148"["title"] + string (18) "Kick line Stop ban"["pid"] = + string (3) "147"["sort"] = + string (1) "0"} [1] = = Array (4{["id"] + = string (3) "149"["title"] + string (12) "Game announcement"["pid"] = + string (3) "147"["sort"] = + string (1) "0"} [2] = = Array (4{["id"] + = string (3) "150"["title"] + string (9) "Gift card"["pid"] = + string (3) "147"["sort"] = + string (1) "0"} [3] = = Array (4{["id"] + = string (3) "151"["title"] + + string (12) "Mail Send"["pid"] = + string (3) "147"["sort"] = + string (1) "0"} [4] = = Array (4{["id"] + = string (3) "152"["title"] + + string (18) "All mail Sent"["pid"] = + string (3) "147"["sort"] = + string (1) "0"} [5] = = Array (4{["id"] + = string (3) "160"["title"] and string (12) "Player adjustment"["pid"] = + string (3) "147"["sort"] = + string (1) "0"} [6] = = Array (4{["id"] + = string (3) "154"["title"] + + string (12) "Approval management"["pid"] = + string (3) "147"["sort"] = + string (1) "0"} [7] = = Array (4{["id"] + = string (3) "155"["title"] + = string (8) "GM Directive"["pid"] = + string (3) "147"["sort"] = + string (1) "0"} [8] = = Array (4{["id"] + = string (3) "156"["title"] + = string (21) "Server Parameter Configuration"["pid"] = + string (3) "147"["sort"] = + string (1) "0"}}} [1] = = Array (5{["id"] + = string (3) "157"["title"] + = string (12) "Log Query"["pid"] = + string (3) "124"["sort"] = + string (1) "0"["child"] = = Array (2) {[0] = = Array (4{["id"] + = string (3) "158"["title"] + = string (12) "Basic Information"["pid"] = + string (3) "157"["sort"] = + string (1) "0"} [1] = = Array (4{["id"] + = string (3) "159"["title"] + + string (12) "Production and elimination Log" ["pid"] + = string (3) "157"  ["sort"] + = string (1) "0" }}} } [3] = = Array (4 ) {["id"] + = string (3) "126"  ["title"] + = string (12) "System Tool"  ["pid"] = Stri Ng (1) "0"  ["sort"] = + string (1) "4" } [4] = = Array (5 ) {["id"] + string (3) "127"  ["title "] = = string (12)" Rights Management " [" pid "] = + string (1)" 0 " [" sort "] + = string (1)" 5 " [" child "] = Arra Y (2 ) {[0] = = Array (4 ) {["id"] + = string (3) "145"  ["title"] + string (12) "User Management"  ["pid"] = = String (3) "127"  ["sort"] + = string (1) "0" } [1] = = Array (4 ) {["id"] + = string (3) "146" span> ["title"] + = string (15) "User group Management"  ["pid"] + string (3) "127"  ["sort"] + = string (1) "0" }} }} 

PHP recursive get unlimited categories menu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.