2017-06-12 108 views
1

如何动态获取类别级数。这是一个PHP数组,当用户改变菜单结构时,这可能会改变,但目前这是一个结构,我只是想知道邮件类别和主类别中多少级别的子类别是“品牌”。这是我的数组结构。如何通过多维数组获取类别级别php

[1] => Array 
    (
     [name] => 01e3f447-4b81-11e7-a858-b8763f541038 
     [nodeInfo] => stdClass Object 
      (
       [idCategory] => 01e3f447-4b81-11e7-a858-b8763f541038 
       [category] => Brands 
       [slug] => brands 
       [description] => 
       [image] => 
       [parentCategory] => 
       [idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038 
       [date] => 2017-06-07 14:58:31 
      ) 

     [children] => Array 
      (
       [0] => Array 
        (
         [name] => 0ded5b28-4b81-11e7-a858-b8763f541038 
         [nodeInfo] => stdClass Object 
          (
           [idCategory] => 0ded5b28-4b81-11e7-a858-b8763f541038 
           [category] => Apparel 
           [slug] => apparel 
           [description] => 
           [image] => 
           [parentCategory] => 01e3f447-4b81-11e7-a858-b8763f541038 
           [idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038 
           [date] => 2017-06-07 14:58:51 
          ) 

         [children] => Array 
          (
           [0] => Array 
            (
             [name] => 3681265e-4b81-11e7-a858-b8763f541038 
             [nodeInfo] => stdClass Object 
              (
               [idCategory] => 3681265e-4b81-11e7-a858-b8763f541038 
               [category] => Adidas 
               [slug] => adidas 
               [description] => 
               [image] => 
               [parentCategory] => 0ded5b28-4b81-11e7-a858-b8763f541038 
               [idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038 
               [date] => 2017-06-07 14:59:59 
              ) 

             [children] => 
            ) 

           [1] => Array 
            (
             [name] => 3f211015-4b81-11e7-a858-b8763f541038 
             [nodeInfo] => stdClass Object 
              (
               [idCategory] => 3f211015-4b81-11e7-a858-b8763f541038 
               [category] => Columbia 
               [slug] => columbia 
               [description] => 
               [image] => 
               [parentCategory] => 0ded5b28-4b81-11e7-a858-b8763f541038 
               [idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038 
               [date] => 2017-06-07 15:00:14 
              ) 

             [children] => 
            ) 

          ) 

        ) 

       [1] => Array 
        (
         [name] => 1988d10a-4b81-11e7-a858-b8763f541038 
         [nodeInfo] => stdClass Object 
          (
           [idCategory] => 1988d10a-4b81-11e7-a858-b8763f541038 
           [category] => Footwear 
           [slug] => footwear 
           [description] => 
           [image] => 
           [parentCategory] => 01e3f447-4b81-11e7-a858-b8763f541038 
           [idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038 
           [date] => 2017-06-07 14:59:11 
          ) 

         [children] => Array 
          (
           [0] => Array 
            (
             [name] => 49becbc8-4b81-11e7-a858-b8763f541038 
             [nodeInfo] => stdClass Object 
              (
               [idCategory] => 49becbc8-4b81-11e7-a858-b8763f541038 
               [category] => Polo 
               [slug] => polo 
               [description] => 
               [image] => 
               [parentCategory] => 1988d10a-4b81-11e7-a858-b8763f541038 
               [idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038 
               [date] => 2017-06-07 15:00:31 
              ) 

             [children] => 
            ) 

           [1] => Array 
            (
             [name] => 4e647bee-4b81-11e7-a858-b8763f541038 
             [nodeInfo] => stdClass Object 
              (
               [idCategory] => 4e647bee-4b81-11e7-a858-b8763f541038 
               [category] => Nike 
               [slug] => nike 
               [description] => 
               [image] => 
               [parentCategory] => 1988d10a-4b81-11e7-a858-b8763f541038 
               [idStatus] => e9d3b949-1301-11e7-a9b8-b8763f541038 
               [date] => 2017-06-07 15:00:39 
              ) 

             [children] => 
            ) 

          ) 

        ) 

      ) 

    ) 
+0

你的问题不明确正是你期待什么。 – kazinayem2011

+0

看看我的阵列结构,你会看到主阵列是品牌和水平是(1)和内部品牌 - >服装哪些级别是(2)和内部品牌 - >服装 - >阿迪达斯哪个级别是(3)这是我究竟想要什么。我想知道邮件类别中的子类别的级别。 –

回答

1

您可以递归做到这一点:

function countChildren($array) { 
     $children = 0; 
     if (isset($array["children"]) { 
      $children = count($array["children"]); 
      foreach ($array["children"] as $subarray) { 
       $children += countChildren($subarray); 
      } 
     } 
     return $children;   
} 

仅计算你能做的水平:

function countChildrenLevels($array) { 
     $children = 0; 
     if (isset($array["children"]) {     
      $sublevels = []; 
      foreach ($array["children"] as $subarray) { 
       $sublevels[] = countChildren($subarray); 
      } 
      $children = 1 + max($sublevels); 
     } 
     return $children;   
} 
+0

非常感谢。我得到了我想要的 –

+0

@MuazzamAli尽快回答可以帮助你的问题得到解决,你应该[接受它](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work#答案-5235)是正确的。 – Tpojka