doxrxwn2252 2017-04-27 03:46
浏览 50
已采纳

如何在PHP / CodeIgniter中构建多维嵌套数组?

Lets say I have a table that has a record of levels that represents a tree heirarchy

id         group       parent_group_id
---------  ----------  ---------------
1          Parent 1    NULL
2          Parent 2    NULL
3          Parent 3    NULL
4          Child 1     1
5          Child 2     2
6          Child 3     2
7          Child 4     6

I need to build a recursive function to build a multidimensional nested array so that it starts out at the "top" by first building the top level arrays of rows that have parent_group_ids of NULL. Fastforward several iterations, im expecting to end up with an object like so

$result = array(
    [0] => array(
        'id' => 1,
        'group' => 'Parent 1',
        'parent_group_id' => NULL,
        'children' => array(
            [0] => array(
                'id' => 4,
                'group' => 'Child 1'
                'parent_group_id' => 1,
                'children' => NULL)),
    [1] => array(
        'id' => 2,
        'group' => 'Parent 2',
        'parent_group_id' => NULL,
        'children' => array(
            [0] => array(
                'id' => 5,
                'group' => 'Child 2'
                'parent_group_id' => 2,
                'children' => NULL),
            [1] => array(
                'id' => 6,
                'group' => 'Child 3'
                'parent_group_id' => 2,
                'children' => array(
                     [0] => array(
                         'id' => 1,
                         'group' => 'Child 4'
                         'parent_group_id' => 6,
                         'children' => NULL)))

What is the best way to build something like this? I need to ensure that it traverses down each "branch". I'm guessing when it gets the id's of the top level parents, it then proceeds to check to see if any rows exist that have a parent_group_id that equals each of the id's from the first run. Then if it finds children, gets the id's of those children and then checks again to see if children exist. And so on and so forth until it runs of out id's to check against.

I am not well-versed with foreach loops to pull off something like this.

  • 写回答

2条回答 默认 最新

  • duanlaiyin2356 2017-04-27 05:27
    关注

    Look at this source code.

    I think this function is somewhat similar to what you are asking.

      public function getAreaTree(array $elements, $parentId = null) {
        $branch = array();
    
        foreach ($elements as $element) {
    
            if ($element['parent_id'] == $parentId) {
    
                $children = getAreaTree($elements, $element['id']);
    
                if ($children) {
    
                    $element['children'] = $children;
    
                }
    
                $branch[] = $element;
            }
    
        }
    
        return empty($branch) ? null : $branch;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 bat批处理,关于数据复制问题
  • ¥50 同步两个不同结果的array中某些属性
  • ¥15 悬赏15远程操控解决问题
  • ¥15 CST复制的模型无法单独修改参数?
  • ¥15 前端页面想做个定时任务,但是使用requestAnimationFrame,setinterval和settimeout都不行
  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)