将表的值放入树中 [英] Getting a table's values into a tree

查看:26
本文介绍了将表的值放入树中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个这样的表:

So, I have a table like such:

id|root|kw1|kw2|kw3|kw4|kw5|name
 1|   A|  B|  C|  D|  E|  F|fileA
 2|   A|  B|   |   |   |   |fileB
 3|   B|  C|  D|  E|   |   |fileC
 4|   A|  B|   |   |   |   |fileD

(几百行...)

我需要把它变成一棵树,如下所示:

And I need to get it into a tree like the following:

*A
 *B
  -fileB
  -fileD
 *C
  *D
   *E
    *F
     -fileA
*B
 *C
  *D
   *E
    -fileC

我很确定桌子的布置很糟糕,但这是我必须忍受的.

I'm pretty sure the table is laid out poorly but it's what I have to live with.

我已经阅读了一些关于邻接表模型和修改了预排序树遍历,但我认为我的数据布局不正确.我认为这需要一个递归函数,但我完全不知道如何去做.

I've read a little about Adjacency List Model & Modified Preorder Tree Traversal but I don't think my data is laid out correctly. I think this requires a recursive function, but I'm not at all sure how to go about that.

我对如何完成这项工作的任何想法持开放态度,即使这意味着将数据提取到一个新表中只是为了处理它.

I'm open to any ideas of how to get this done even if it means extracting the data into a new table just to process this.

是否有任何好的选择或任何好的方法可以做到这一点?(例子当然是加分项)

Are there any good options available to me or any good ways to do this? (Examples are a bonus of course)

推荐答案

这是我能想到的最简单的工作解决方案.

Here's the simplest working solution I could come up with.

假设:

  1. 你有一个数组数组(你的结果集);它被命名为 $rows.
  2. 您在上面给出的结果集中的空列值等于 null.
  3. 树中没有分支的名称是整数的字符串表示.

代码:

$tree = array();

foreach($rows as $row) {
    // Second parameter: array of 6 items as per your sample result set
    place_in_tree($tree, array($row['root'], ... $row['kw5']), $row['file']);
}

function place_in_tree(array $tree, array $path, $item) {
    // While there are more branches to be taken in $path
    while(($branch = array_shift($path)) !== null) {
        // Create the new branch if it doesn't exist
        if(!isset($tree[$branch])) {
            $tree[$branch] = array();
        }

        // Select the subtree in that branch for the next iteration
        $tree = $tree[$branch];
    }

    // Finally, add the item
    $tree[] = $item;
}

这将创建一个带有嵌套数组的数组.该数组包含许多带有字符串键的项(这些是分支",属于 array 类型)和一些带有数字键的项(这些是文件",属于 array 类型)代码>字符串).子数组的填充方式相同.

This creates an array with nested arrays. This array contains a number of items with string keys (these are "branches", and are of type array) and a number of items with numeric keys (these are "files", and are of type string). Child arrays are populated in the same way.

如果您需要的东西比大喇叭数组更接近您的业务模型,您可以在上面的 place_in_tree 函数中调整分支选择和项目存储逻辑.

If you require something more close to your business model than a big honking array, you can tweak the branch selection and item storage logic in function place_in_tree above.

另外,如果上面的假设 #3 在你的情况下不成立,你需要以同样的方式参与一些,要么选择一种明确的方式来区分树的树枝和树叶,要么选择另一个结构来表示它.

Also, if assumption #3 above does not hold in your case, you will need to get a little involved in the same manner, either choosing a non-ambiguous way to tell apart the branches and leaves of the tree, or choosing another structure to represent it.

这篇关于将表的值放入树中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆