从数据库构建树

时间:2018-09-11 20:41:06

标签: 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;
}

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

最诚挚的问候。

2 个答案:

答案 0 :(得分:1)

你在这里

$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

如果您希望它的组织性更好一些,可以先为孩子设置钥匙,然后再进行其他操作。像这样

 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(如果未列出该ID,则是什么级别)。

答案 1 :(得分:0)

基于@ArtisticPhoenix代码。

代码如下。

private void button1_Click(object sender, EventArgs e)
{
    Bubble bubble = new Bubble("Help me, I'm Fading!", 1000, 2000);
    bubble.Show();
}

感谢所有支持。

最诚挚的问候。