php - 从数据库构建树

标签 php algorithm

最近接到一个工作任务,组织一个数组,数据库组织的不好。

数据为例:

ID           LEVEL
1000000000     1
1100000000     2
1110000000     3
1111000000     4
1111010000     5
1111010001     6
1111010002     6
1111010003     6
1120000000     3
1121020037     6
1123000000     4
2000000000     1

我必须按级别组织它并在较低级别中添加较大级别。这里的主要目标是创建一个树,以扩展数据和数字。

1 - 1000000000
1.1 - 1100000000
1.1.1 - 1110000000
1.2 - 1200000000
1.2.1 - 1210000000
2 - 2000000000

我正在努力完成一个多星期。

function buildTree(array &$elements, $parentId = 0) {
    $branch = array();
    foreach ($elements as $element) {
        $cc = preg_replace("/0+$/", "", $element['cd_conta_estrutural']);
        if ($cc == $parentId) {
            $children = $this->buildTree($elements, $cc);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[$cc] = $element;
            unset($elements[$cc]);
        }
    }
    return $branch;
}

如果不可能,还有其他选择吗?

最好的问候。

最佳答案

给你

$a = [
    ['id'=>1000000000,'level'=>1],
    ['id'=>1100000000,'level'=>2],
    ['id'=>1110000000,'level'=>3],
    ['id'=>1111000000,'level'=>4],
    ['id'=>1111010000,'level'=>5],
    ['id'=>1111010001,'level'=>6],
    ['id'=>1111010002,'level'=>6],
    ['id'=>1111010003,'level'=>6],
    ['id'=>1120000000,'level'=>3],
    ['id'=>1121020037,'level'=>6],
    ['id'=>1123000000,'level'=>4],
    ['id'=>2000000000,'level'=>1],
];


function makeTree($array, $level=1){
    $branch = [];
    foreach($array as $item){
        if($item['level'] == $level){
             $branch[] = $item;
        }else if($item['level'] > $level){
            $branch['children'] = [];
            $branch['children'] = array_merge($branch['children'],makeTree($array,$level+1));
        }

    }
    return $branch;
}

print_r(makeTree($a));

输出

Array
(
    [0] => Array
        (
            [id] => 1000000000
            [level] => 1
        )

    [children] => Array
        (
            [0] => Array
                (
                    [id] => 1100000000
                    [level] => 2
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 1110000000
                            [level] => 3
                        )

                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 1111000000
                                    [level] => 4
                                )

                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1111010000
                                            [level] => 5
                                        )

                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 1111010001
                                                    [level] => 6
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 1111010002
                                                    [level] => 6
                                                )

                                            [2] => Array
                                                (
                                                    [id] => 1111010003
                                                    [level] => 6
                                                )

                                            [3] => Array
                                                (
                                                    [id] => 1121020037
                                                    [level] => 6
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 1123000000
                                    [level] => 4
                                )

                        )

                    [1] => Array
                        (
                            [id] => 1120000000
                            [level] => 3
                        )

                )

        )

    [1] => Array
        (
            [id] => 2000000000
            [level] => 1
        )

)

Sandbox

如果你想让它组织得更好一点,你可以先为 child 设置 key 。像这样

 function makeTree($array, $level=1){
   $branch = ['children' => []];
    foreach($array as $item){
        if($item['level'] == $level){
             $branch[] = $item;
        }else if($item['level'] > $level){
             $branch['children'] = array_merge($branch['children'],makeTree($array,$level+1));
        }
    }
    return $branch;
 }

就我个人而言,我会为当前级别添加一个键。

 function makeTree($array, $level=1){
   $branch = ['leafs' => [], 'children' => []];
    foreach($array as $item){
        if($item['level'] == $level){
             $branch['leafs'][] = $item;
        }else if($item['level'] > $level){
             $branch['children'] = array_merge($branch['children'],makeTree($array,$level+1));
        }
    }
    return $branch;
 }

这给了你这个

Array
(
    [leafs] => Array
        (
            [0] => Array
                (
                    [id] => 1000000000
                    [level] => 1
                )

            [1] => Array
                (
                    [id] => 2000000000
                    [level] => 1
                )

        )

    [children] => Array
        (
            [leafs] => Array
                (
                    [0] => Array
                        (
                            [id] => 1100000000
                            [level] => 2
                        )

                )

            [children] => Array
                (
                    [leafs] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 1110000000
                                    [level] => 3
                                )

                            [1] => Array
                                (
                                    [id] => 1120000000
                                    [level] => 3
                                )

                        )

                    [children] => Array
                        (
                            [leafs] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1111000000
                                            [level] => 4
                                        )

                                    [1] => Array
                                        (
                                            [id] => 1123000000
                                            [level] => 4
                                        )

                                )

                            [children] => Array
                                (
                                    [leafs] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 1111010000
                                                    [level] => 5
                                                )

                                        )

                                    [children] => Array
                                        (
                                            [leafs] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 1111010001
                                                            [level] => 6
                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 1111010002
                                                            [level] => 6
                                                        )

                                                    [2] => Array
                                                        (
                                                            [id] => 1111010003
                                                            [level] => 6
                                                        )

                                                    [3] => Array
                                                        (
                                                            [id] => 1121020037
                                                            [level] => 6
                                                        )

                                                )

                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

但无论你想要什么。

我应该提一下,如果你错过了一个关卡,它不会中断,它只会为那个关卡放一个空的关卡。有一些可以避免的工作,但问题中并没有具体说明。

您的尝试非常接近,您只是用 ID 代替了关卡。我认为您并不真正关心 ID 是什么,例如 ['id'=>2000000000,'level'=>1] 这不在级别 #2 中。另一个例子是这个 ID 1111010003 如果不是列出的级别,那是什么级别。

关于php - 从数据库构建树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52283908/

相关文章:

javascript - 算法:每组数字加起来等于某个数字

algorithm - 一个非常大的阶乘的最后一个非零数字

php - Android - Kotlin : Sending a non empty jsonObject but receiving an empty one

php - Eclipse PDT 中的 HTML 所见即所得编辑器

php - 数据库条目被删除

php - 将图像从 PHP Web 服务发送到移动客户端

php - 从mysql接收jquery进度条值

javascript - 将一系列值映射到另一个值

javascript - 这种排序可以更优雅/递归吗

python - 寻找最小差异