string(1) "1" ["pid"] => string(1) "0" "/> string(1) "1" ["pid"] => string(1) "0" ">
Maison > développement back-end > tutoriel php > php怎么遍历交叉的多维数组并给其id和pid(父键id)

php怎么遍历交叉的多维数组并给其id和pid(父键id)

WBOY
Libérer: 2016-06-13 13:18:22
original
977 Les gens l'ont consulté

php如何遍历交叉的多维数组并给其id和pid(父键id)

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> 
array(7) {
  [1] => array(5) {
    ["id"] => string(1) "1"
    ["pid"] => string(1) "0"
    ["name"] => string(15) "AssetManagement"
    ["title"] => string(12) "资产管理"
    ["submenu"] => array(1) {
      [0] => string(0) ""
    }
  }
  [2] => array(5) {
    ["id"] => string(1) "2"
    ["pid"] => string(1) "0"
    ["name"] => string(14) "SystemSettings"
    ["title"] => string(12) "系统设置"
    ["submenu"] => array(1) {
      [0] => string(0) ""
    }
  }
  [3] => array(5) {
    ["id"] => string(1) "3"
    ["pid"] => string(1) "0"
    ["name"] => string(19) "Management"
    ["title"] => string(12) "后台管理"
    ["submenu"] => array(1) {
      [0] => string(0) ""
    }
  }
  [4] => array(5) {
    ["id"] => string(2) "11"
    ["pid"] => string(1) "1"
    ["name"] => string(9) "HZ"
    ["title"] => string(15) "HZ"
    ["submenu"] => array(1) {
      [0] => string(0) ""
    }
  }
  [5] => array(5) {
    ["id"] => string(2) "10"
    ["pid"] => string(1) "1"
    ["name"] => string(9) "XM"
    ["title"] => string(15) "XM"
    ["submenu"] => array(1) {
      [0] => string(0) ""
    }
  }
  [6] => array(5) {
    ["id"] => string(1) "9"
    ["pid"] => string(1) "1"
    ["name"] => string(9) "BJ"
    ["title"] => string(15) "BJ"
    ["submenu"] => array(1) {
      [0] => string(0) ""
    }
  }
  
  [7] => array(5) {
    ["id"] => string(1) "4"
    ["pid"] => string(1) "1"
    ["name"] => string(20) "TotalManagement"
    ["title"] => string(15) "总管理"
    ["submenu"] => array(3) {
      [0] => array(5) {
        ["id"] => string(1) "5"
        ["name"] => string(12) "PC"
        ["menu_id"] => string(1) "4"
        ["title"] => string(17) "PC"
        ["access"] => int(1)
      }
      [1] => array(5) {
        ["id"] => string(1) "6"
        ["name"] => string(13) "BJB"
        ["menu_id"] => string(1) "4"
        ["title"] => string(18) "BJB"
        ["access"] => int(1)
      }
      [2] => array(5) {
        ["id"] => string(1) "7"
        ["name"] => string(16) "Select"
        ["menu_id"] => string(1) "4"
        ["title"] => string(15) "查询"
        ["access"] => int(1)
      }
    }
  }
  
}

怎么把上面的数组写成下面那样?


array(3) {
  [1] => array(5) {
    ["id"] => string(1) "1"
    ["pid"] => string(1) "0"
    ["name"] => string(15) "AssetManagement"
    ["title"] => string(12) "资产管理"
    ["submenu"] => array(4) {
    
          [1] => array(5) {
            ["id"] => string(2) "11"
            ["pid"] => string(1) "1"
            ["name"] => string(9) "HZ"
            ["title"] => string(15) "HZ"
            ["submenu"] => array(1) {
              [0] => string(0) ""
            }
          }
          [2] => array(5) {
            ["id"] => string(2) "10"
            ["pid"] => string(1) "1"
            ["name"] => string(9) "XM"
            ["title"] => string(15) "XM"
            ["submenu"] => array(1) {
              [0] => string(0) ""
            }
          }
          [3] => array(5) {
            ["id"] => string(1) "9"
            ["pid"] => string(1) "1"
            ["name"] => string(9) "BJ"
            ["title"] => string(15) "BJ"
            ["submenu"] => array(1) {
              [0] => string(0) ""
            }
          }
          
          [4] => array(5) {
            ["id"] => string(1) "4"
            ["pid"] => string(1) "1"
            ["name"] => string(20) "TotalManagement"
            ["title"] => string(15) "总管理"
            ["submenu"] => array(3) {
              [0] => array(5) {
                ["id"] => string(1) "5"
                ["name"] => string(12) "PC"
                ["menu_id"] => string(1) "4"
                ["title"] => string(17) "PC"
                ["access"] => int(1)
              }
              [1] => array(5) {
                ["id"] => string(1) "6"
                ["name"] => string(13) "BJB"
                ["menu_id"] => string(1) "4"
                ["title"] => string(18) "BJB"
                ["access"] => int(1)
              }
              [2] => array(5) {
                ["id"] => string(1) "7"
                ["name"] => string(16) "Select"
                ["menu_id"] => string(1) "4"
                ["title"] => string(15) "查询"
                ["access"] => int(1)
              }
            }
          }
        
    
    
    
    
    
    }
  }
  [2] => array(5) {
    ["id"] => string(1) "2"
    ["pid"] => string(1) "0"
    ["name"] => string(14) "SystemSettings"
    ["title"] => string(12) "系统设置"
    ["submenu"] => array(1) {
      [0] => string(0) ""
    }
  }
  [3] => array(5) {
    ["id"] => string(1) "3"
    ["pid"] => string(1) "0"
    ["name"] => string(19) "Management"
    ["title"] => string(12) "后台管理"
    ["submenu"] => array(1) {
      [0] => string(0) ""
    }
  }
  
  
}

 <div class="clear">
                 
              
              
        
            </div>
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal