编程语言
首页 > 编程语言> > 将php数组重新排列为嵌套的分层数组

将php数组重新排列为嵌套的分层数组

作者:互联网

如何使用php将array#1转换为array#2结构?

第一个数组是对生物列表进行数据库查询的结果,每个生物都按顺序,科,属,种进行分类.从层次上讲,种是各种属的子分类,而种属是各种科的子分类等.

用命名空间术语,您可以这样阅读:

索引[0]处的项目—> Hemiptera.Miridae.Kanakamiris
 索引[1]处的项目—> Hemiptera.Miridae.Neophloeobia.incisa

数组#1的键之间存在一种父/子关系,如下所示:

>“ Rank_Order”值是“ Rank_Family”值的父级
>“ Rank_Family”值是“ Rank_Genus”值的父级
>“ Rank_Genus”值是“ Rank_Species”值的父级

阵列#1:

Array
(
    [0] => Array
        (
            ['Rank_Order'] => 'Hemiptera'
            ['Rank_Family'] => 'Miridae'
            ['Rank_Genus'] => 'Kanakamiris'
            ['Rank_Species'] => ''
        )   
    [1] => Array
        (
            ['Rank_Order'] => 'Hemiptera'
            ['Rank_Family'] => 'Miridae'
            ['Rank_Genus'] => 'Neophloeobia'
            ['Rank_Species'] => 'incisa'
        )
    [2] => Array
        (
            ['Rank_Order'] => 'Hemiptera'
            ['Rank_Family'] => 'Noridae'
            ['Rank_Genus'] => 'Canelbia'
            ['Rank_Species'] => 'Arissa'
        )
)

以下数组是我需要的数组结构:
阵列#2:

Array(
    [name]     => 'Hemiptera'
    [children] => Array(

        [0] => Array(
            [name]     => 'Miridae'
            [children] => Array(

                [0] => Array(
                    [name]     => 'Kanakamiris'
                    [children] => Array(
                    )
                )
                [1] => Array(
                    [name]     => 'Neophloeobia'
                    [children] => Array(

                        [0] => Array(
                            [name] => 'incisa'
                            [children] => Array(
                            )
                        )
                    )
                )
            )
        )
        [1] => Array(
            [name]     => 'Noridae'
            [children] => Array(

                [0] => Array(
                    [name]     => 'Canelbia'
                    [children] => Array(

                        [0] => Array(
                            [name] => 'Arissa'
                            [children] => Array(
                            )
                        )
                    )
                )
            )
        )
    )
)

我在堆栈溢出中看到了类似的问题,尽管在我的情况下无法使用它们.例如. php-reorder-array-to-reflect-parent-id-hierarchy

解决方法:

我认为这对于真正的大型阵列而言不是超级有效,但它适用于您的情况(here’s a sample).

 $array = ...
 $levels = array('Rank_Order', 'Rank_Family', 'Rank_Genus', 'Rank_Species');

 function get_children($parent, $lev, $orig, $levels){
     if($lev + 1 > count($levels)){
          return array();
     }

     $seen = array();
     $children = array();
     foreach($orig as $node){
         if($node[$levels[$lev]] == $parent && !in_array($node[$levels[$lev+1]], $seen)){
             $seen[] = $node[$levels[$lev+1]];
             $children[] = get_children($node[$levels[$lev+1]], $lev+1, $orig, $levels);
         }
     }
     return array('name' => $parent, 'children' => $children);
 }

 function hier($orig, $levels){
     $seen = array();
     $result = array();
     foreach($orig as $node){
         if(!in_array($node[$levels[0]], $seen)){
              $seen[] = $node[$levels[0]];
              $result[] = get_children($node[$levels[0]], 0, $orig, $levels);
         }
     }
     return $result;
 }

 print_r($array);
 print_r(hier($array, $levels));

标签:taxonomy,php,arrays,hierarchy
来源: https://codeday.me/bug/20191014/1912620.html