如何将数组放入数组但像树一样

Posted

技术标签:

【中文标题】如何将数组放入数组但像树一样【英文标题】:How to make arrays in to arrays but like tree 【发布时间】:2021-12-28 11:50:59 【问题描述】:

我有

`<?php 
$arr = [['id'=> 1, 'parent_id' => '-', 'name' => 'id1'],
['id' => 2, 'parent_id' => 1, 'name'=> 'id2' ],
['id' => 3, 'parent_id' => 1, 'name'=> 'id3' ],
['id' => 4, 'parent_id' => '-', 'name'=> 'id4' ]
,['id' => 5,'parent_id' => 2, 'name'=> 'id5' ],
['id' => 6, 'parent_id' => 3, 'name'=> 'id6' ],
['id' => 7, 'parent_id' => '-', 'name'=> 'id7' ],
['id' => 8, 'parent_id' => 3, 'name'=> 'id8' ],
['id' => 9, 'parent_id' => 4, 'name'=> 'id9' ], 
['id' => 10, 'parent_id' => 9, 'name'=> 'id10' ]];
// 1-2-5
// |-3-6
//---|-8
//------
// 4-9-10
//---
// 7
$new = array();
foreach ($arr as $a)
    $new[$a['parent_id']][] = $a;

$tree = createTree($new, array($arr[0]));
print_r($tree);

function createTree(&$list, $parent)
    $tree = array();
    foreach ($parent as $k=>$l)
        if(isset($list[$l['id']]))
            $l['children'] = createTree($list, $list[$l['id']]);
        
        $tree[] = $l;
        
     
    return $tree;


`

我的问题是如何将数组列表写入数组,但就像一棵树一样,我设法做了第一个家庭。

所以我的问题是这个递归操作在第一个 'parent_id'=> '-' 之后不会重复,它不会转到其他 parent_id 希望获得任何帮助的解释。

【问题讨论】:

欢迎来到 SO!也许是我,但我不完全理解你的问题。你能澄清一下吗? 已经从 omarboussarsar 得到答案,但我希望看到更多可以用更少代码完成的答案 【参考方案1】:

此解决方案可以帮助您。

function buildTree(array $elements, ?int $parentId = null): array

    $branch = [];
    foreach ($elements as $element) 
        if ($element['parent_id'] === $parentId) 
            $children = buildTree($elements, $element['id']);
            if ($children) 
                $element['children'] = $children;
            
            $branch[] = $element;
        
    

    return $branch;



$array = [
    ['id'=> 1, 'parent_id' => '-', 'name' => 'id1'],
    ['id' => 2, 'parent_id' => 1, 'name'=> 'id2'],
    ['id' => 3, 'parent_id' => 1, 'name'=> 'id3'],
    ['id' => 4, 'parent_id' => '-', 'name'=> 'id4'],
    ['id' => 5,'parent_id' => 2, 'name'=> 'id5'],
    ['id' => 6, 'parent_id' => 3, 'name'=> 'id6'],
    ['id' => 7, 'parent_id' => '-', 'name'=> 'id7'],
    ['id' => 8, 'parent_id' => 3, 'name'=> 'id8'],
    ['id' => 9, 'parent_id' => 4, 'name'=> 'id9'], 
    ['id' => 10, 'parent_id' => 9, 'name'=> 'id10'],
];

$tree = [];
foreach ($array as $element) 
    if ($element['parent_id'] === '-') 
        $tree[] = [
            'id' => $element['id'],
            'name' => $element['name'],
            'children' => buildTree($array, $element['id']),
        ];
    


树的 print_r:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => id1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [parent_id] => 1
                            [name] => id2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 5
                                            [parent_id] => 2
                                            [name] => id5
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [parent_id] => 1
                            [name] => id3
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 6
                                            [parent_id] => 3
                                            [name] => id6
                                        )

                                    [1] => Array
                                        (
                                            [id] => 8
                                            [parent_id] => 3
                                            [name] => id8
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 4
            [name] => id4
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 9
                            [parent_id] => 4
                            [name] => id9
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 10
                                            [parent_id] => 9
                                            [name] => id10
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 7
            [name] => id7
            [children] => Array
                (
                )

        )

)

如果你想测试,我把代码放在这里:http://sandbox.onlinephpfunctions.com/code/ff22978371819db8e890a404e41b046bc613b7dd

我希望我是有用的。祝你好运:)

【讨论】:

【参考方案2】:

这一行:

$tree = createTree($new, array($arr[0]));

仅创建来自arr[0] 的后代列表。

要获取整棵树,需要遍历根节点列表:

foreach ($new['-'] as $root) 
$tree = createTree($new, array($root));
print_r($tree);

Teh playground

【讨论】:

以上是关于如何将数组放入数组但像树一样的主要内容,如果未能解决你的问题,请参考以下文章

sql查询出来的结果,如何放入数组中

如何将pdo返回的关联数组的内容放入数组中

如何将发布数据放入 CodeIgniter 中的数组中?帖子项目是数组

PHP POST Curl 标头,如何将数组放入数组中? [复制]

如何使用 for 循环将一维数组的值放入 C++ 中的二维数组

如何将递归函数的答案放入数组(PHP)中?