将表的值放入树中

发布于 2024-09-02 02:01:38 字数 589 浏览 2 评论 0原文

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

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

几百行......)

我需要将它放入如下所示的树中:

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

我很确定该表的布局很糟糕,但这是我必须忍受的。

我读过一些有关邻接列表模型和邻接列表模型的内容。修改了预序树遍历,但我认为我的数据布局不正确。我认为这需要一个递归函数,但我完全不确定如何去做。

我对如何完成此任务的任何想法持开放态度,即使这意味着将数据提取到新表中只是为了处理此问题。

我有什么好的选择或者有什么好的方法可以做到这一点吗? (当然,例子是额外的奖励)

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

(several hundred rows...)

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)

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

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

发布评论

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

评论(1

南七夏 2024-09-09 02:01:38

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

假设:

  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)和一些带有数字键的项目(这些是“文件”,类型为 <代码>字符串)。子数组以相同的方式填充。

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

另外,如果上面的假设 3 不适合您的情况,您将需要以相同的方式进行一些参与,要么选择一种明确的方式来区分树的分支和叶子,要么选择另一种结构来区分树的分支和叶子。代表它。

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

Assumptions:

  1. You have an array of arrays (your result set); it is named $rows.
  2. Empty column values in the result set you give above are equal to null.
  3. There are no branches in your tree whose names are string representations of integers.

The code:

$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;
}

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.

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.

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.

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