PHP 通过遍历嵌套树模型构建数组

发布于 2024-12-06 08:27:00 字数 1964 浏览 0 评论 0原文

我有一个典型的嵌套树模型,我想根据级别或深度构建一个带有“子”数组的数组,但它似乎对我不起作用。这就是我现在所拥有的:

while($this->tax->getTreeNext($nodes)) 
{

    $level = $this->tax->getTreeLevel($nodes);

    if($level != 0){
        echo $level . '-' . $current_level;
        if($level > $current_level){
            $terms[$i] = array(
                    'term_id' => $terms[$i-1]['term_id'],
                    'name' => $terms[$i-1]['name'],
                    'level' => $terms[$i-1]['level'],
                        'children'  => array(
                        'term_id'   => $nodes['row']['term_id'], 
                        'name'      => $nodes['row']['name'], 
                        'level'     => $level,               
                        )
                );

            unset($terms[$i-1]);
        }else{

            $terms[$i] = array(
                'term_id'   => $nodes['row']['term_id'], 
                'name'      => $nodes['row']['name'], 
                'level'     => $level
            );
        }

        $current_level = $level;
        $i++;
    }
}

这适用于单个孩子,但如果孩子有孩子则不行......有什么建议如何解决这个问题吗?

谢谢你!

编辑:

这是似乎接近工作的最新版本:

function process(&$arr, &$prev_sub = null, $cur_depth = 1) {

  $cur_sub = array();
    while($line = current($arr)){
        if($line['depth'] < $cur_depth){
            return $cur_sub; 
        }elseif($line['depth'] > $cur_depth){


            $prev_sub = $this->process($arr, $cur_sub, $cur_depth + 1 );

        }else{

            $cur_sub[$line['term_id']] = array('term_id' => $line['term_id'], 'name' => $line['name']);
            $prev_sub =& $cur_sub[$line['term_id']];
            next($arr);
        }
    }
  return $cur_sub;
 }

树被传递到其中,并带有与每个节点关联的深度值。当前的问题在于 elseif($line['depth'] > $cur_depth)。如果节点有子节点,则它仅返回子节点的数组,但不包含该节点名称或 term_id。

谢谢你!

I have a typical nested tree model and I want to build an array with a 'children' array based on the levels or depths, but it doesn't seem to be working for me. Here's what I have now:

while($this->tax->getTreeNext($nodes)) 
{

    $level = $this->tax->getTreeLevel($nodes);

    if($level != 0){
        echo $level . '-' . $current_level;
        if($level > $current_level){
            $terms[$i] = array(
                    'term_id' => $terms[$i-1]['term_id'],
                    'name' => $terms[$i-1]['name'],
                    'level' => $terms[$i-1]['level'],
                        'children'  => array(
                        'term_id'   => $nodes['row']['term_id'], 
                        'name'      => $nodes['row']['name'], 
                        'level'     => $level,               
                        )
                );

            unset($terms[$i-1]);
        }else{

            $terms[$i] = array(
                'term_id'   => $nodes['row']['term_id'], 
                'name'      => $nodes['row']['name'], 
                'level'     => $level
            );
        }

        $current_level = $level;
        $i++;
    }
}

This works for a single child, but not if the children have children... any suggestions how to fix this?

Thank you!

Edit:

This is the latest that appears to be close to working:

function process(&$arr, &$prev_sub = null, $cur_depth = 1) {

  $cur_sub = array();
    while($line = current($arr)){
        if($line['depth'] < $cur_depth){
            return $cur_sub; 
        }elseif($line['depth'] > $cur_depth){


            $prev_sub = $this->process($arr, $cur_sub, $cur_depth + 1 );

        }else{

            $cur_sub[$line['term_id']] = array('term_id' => $line['term_id'], 'name' => $line['name']);
            $prev_sub =& $cur_sub[$line['term_id']];
            next($arr);
        }
    }
  return $cur_sub;
 }

The tree is passed into this with depth values associated to each node. The current problem is with the elseif($line['depth'] > $cur_depth). If a node has children, it only returns arrays for the children, but it does not include that nodes name or term_id.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2024-12-13 08:27:00

由于我不太了解您当前的数据结构是什么样子,请看一下这个遍历树的简单示例。

$treeRoot = $this->tax->getRoot();

$result = traverse($treeRoot, 0);

function traverse($root, $level){
 $arr = array();

 $arr['term_id'] = $root['row']['term_id'];
 $arr['name'] = $root['row']['name'];
 $arr['level'] = $level;

 while($child = $root->getNextChild()){
  $arr['children'][] = traverse($child, $level+1);
 }

 return $arr;
}

因此,您从树根开始,填充数组的第一层。然后你继续处理 root 的孩子,但你会更深入一层。您执行与根完全相同的操作,填充数据并转到子项的子项。当您到达树的底部时,最后一个(祖父孙子)孩子发现它没有剩下的孩子,因此它只是将自身(普通数组)返回给其父级。该父级将自身返回到其父级,依此类推,直到再次到达根。

瞧,您已经有了一个嵌套数组。通常,您会将这种结构保留为树,但由于我不知道您想要的结果到底是什么(您没有提供示例),因此请使用上面的代码作为您自己的实现的参考。

Since I do not really get how your current data structures looks like, take a look at this trivial example of traversing a tree.

$treeRoot = $this->tax->getRoot();

$result = traverse($treeRoot, 0);

function traverse($root, $level){
 $arr = array();

 $arr['term_id'] = $root['row']['term_id'];
 $arr['name'] = $root['row']['name'];
 $arr['level'] = $level;

 while($child = $root->getNextChild()){
  $arr['children'][] = traverse($child, $level+1);
 }

 return $arr;
}

So you start at the tree root, and fill the first level of array. Then you continue with root's children, but you go one level deeper. You do exactly the same thing as with the root, you fill in the data and go to children's children. When you reach the bottom of the tree, the last (grandgrandgrand)child finds out it's got no children left, so it just returns itself(normal array) to its parent. That parent returns itself back to its parent, and so on, till you reach the root again.

And voilà, you've got yourself a nested array. Typically you'd leave this kind of structure as a tree, but since I do not know what exactly do you want your result to be (your provided no example), use the above code as a reference for your own implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文