Php 从数据库构建树

Php 从数据库构建树,php,algorithm,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

我最近接到一个组织数组的任务,数据库组织得不好

以数据为例:

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
        )

)

如果你想让它组织得更好一点,你可以先为孩子们设置钥匙。像这样

 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替换为level。我认为您并不真正关心ID是什么,例如
['ID'=>2000000000,'level'=>1]
这不在level#2中。另一个例子是这个ID
1111010003
如果不是列出的ID,那么它是什么级别。

基于@artisticfenix code

代码的结尾如下

private function buildTree(&$dados, $level = 1) {    
    $branch = [];    
    foreach($dados as $i => $item) {

        $cc = substr($item['cd_conta_estrutural'], 0, $level);

        if (!isset($dados[$i])) continue;    

        if ($item['id_nivel'] == $level) {    
            $branch[$cc] = $item;    
            unset($dados[$i]);    
        }
        else if ($item['id_nivel'] > $level) {    
            $branch[$cc]['children'] = [];    
            $branch[$cc]['children'] = array_merge($branch[$cc]['children'], $this->buildTree($dados,$level + 1));

        }
        else if ($item['id_nivel'] < $level)
            return $branch;
    }
    return $branch;
}
私有函数构建树(&$dados,$level=1){
$branch=[];
foreach($i=>$item的护墙板){
$cc=子项($item['cd\u conta\u estrutural',0,$level);
如果(!isset($dados[$i])继续;
如果($item['id_nivel']=$level){
$branch[$cc]=$item;
未设置($dados[$i]);
}
如果($item['id_nivel']>$level){
$branch[$cc]['children']=[];
$branch[$cc]['children']=array_merge($branch[$cc]['children'],$this->buildTree($dados,$level+1));
}
其他如果($item['id\u nivel']<$level)
返回$branch;
}
返回$branch;
}
谢谢大家的支持


敬请注意。

这棵树应该有多深?深度为10?我可以按照您的树示例一直到1.1.1,1111010000如何转换为2-2000000000?它将一直到级别6。当它为2时,意味着它重新启动。我收到的数据不是很好,这就是为什么有必要创建家长和孩子。当它跳跃时,意味着在3以下没有其他的,只有6。我很难解释,但主要目标是进入关卡,我试图分解字符,然后按关卡的编号进行迭代。看看Java中的代码,它可能会给你一个提示:您好,很抱歉回答得太晚。我正在研究你的代码,它是有效的,我试图做的主要功能是保持数组中的级别,而不是通过分隔值。如下所示:[0]=>[all_data,childrens=>[level2,childres=>level3]]。有了你的代码,我想我可以配置它,因为我这次需要它。