php - 在 PHP 中使用 parentIds 创建多级数组

标签 php arrays

我正在尝试设置一个可以具有多个级别的列表,使用 parentId 来定义其父级。第一项的 parentId 为 NULL。一些条目的示例:

<strong>id parentId name</strong>
1    NULL     item1
2    NULL     item2
3    1        item3
4    2        item4
5    3        item5
6    3        item6

因此,1 和 2 是主要项目; 3 是 1 的 child ; 4 是 2 岁的 child ; 5 是 3 的 child (它本身是 1 的 child ); 6 也是 3 的 child (它本身是 1 的 child );等等

我无法创建一个数组来正确地将这些项目添加到正确的级别。它应该看起来像这样:


Array
(
    [1] => Array
        (
            [name] => item1
            [parentId] => 
            [children] => Array
                (
                    [3] => Array
                        (
                            [name] => item3
                            [parentId] => 1
                            [children] => Array
                                (
                                    [5] => Array
                                        (
                                            [name] => item5
                                            [parentId] => 3
                                        )

                                    [6] => Array
                                        (
                                            [name] => item6
                                            [parentId] => 3
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [name] => item2
            [parentId] => 
            [children] => Array
                (
                    [4] => Array
                        (
                            [name] => item4
                            [parentId] => 2
                        )

                )

        )

)

但是假设我使用 foreach() 遍历所有项目,然后我到达项目 5。它的 parentId 是 3,但此时,我不知道这个父 3 位于何处在数组中,以及如何将子项添加到该父项。

是否有循环遍历这些项目并以正确方式将它们全部放置到位的技巧?

最佳答案

来了

// your original data as an array
$data = array(
    array(
        'id' => 1,
        'parentId' => null,
        'name' => 'item1'
    ),
    array(
        'id' => 2,
        'parentId' => null,
        'name' => 'item2'
    ),
    array(
        'id' => 3,
        'parentId' => 1,
        'name' => 'item3'
    ),
    array(
        'id' => 4,
        'parentId' => 2,
        'name' => 'item4'
    ),
    array(
        'id' => 5,
        'parentId' => 3,
        'name' => 'item5'
    ),
    array(
        'id' => 6,
        'parentId' => 3,
        'name' => 'item6'
    ),
);

一个递归函数

function buildTree( $ar, $pid = null ) {
    $op = array();
    foreach( $ar as $item ) {
        if( $item['parentId'] == $pid ) {
            $op[$item['id']] = array(
                'name' => $item['name'],
                'parentId' => $item['parentId']
            );
            // using recursion
            $children =  buildTree( $ar, $item['id'] );
            if( $children ) {
                $op[$item['id']]['children'] = $children;
            }
        }
    }
    return $op;
}

print_r( buildTree( $data ) );

/*
Array
(
    [1] => Array
        (
            [name] => item1
            [parentId] => 
            [children] => Array
                (
                    [3] => Array
                        (
                            [name] => item3
                            [parentId] => 1
                            [children] => Array
                                (
                                    [5] => Array
                                        (
                                            [name] => item5
                                            [parentId] => 3
                                        )

                                    [6] => Array
                                        (
                                            [name] => item6
                                            [parentId] => 3
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [name] => item2
            [parentId] => 
            [children] => Array
                (
                    [4] => Array
                        (
                            [name] => item4
                            [parentId] => 2
                        )

                )

        )

)
*/

关于php - 在 PHP 中使用 parentIds 创建多级数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2273449/

相关文章:

php - Buddypress - 在标题中显示通知和配置文件(而不是 WP-admin 栏)

php - XML - CSV 数据输出 : server-client interaction and optimization

c - 从内存中的特定地址开始初始化数组 - C编程

c++ - 带有大数组的 C++ 中的堆栈溢出

c# - 获取在数组中的文本框中输入的数字

arrays - 将 wchar_t[] 字符串的元素添加到 array<wchar_t>^ 而不使用 for 循环

javascript - 根据价格计算图层数量

php - Laravel 关系数据库连接

php - POST 不检查按钮表单提交

iOS 在展开可选值时意外发现 nil