使用递归来展平带有基于值的子项的 PHP 数组(为此浪费了一个小时!)

发布于 2024-09-28 19:59:51 字数 13163 浏览 4 评论 0原文

我正在尝试构建一个导航菜单。我收到一个具有如下结构的数组:

[ 
  [
    Title = A
    Sub items = [
      Title = B
      Sub items = [
        Title = C
      ]
    ]
  ],
  [
    Title = A
    Sub items = [
      Title = B
      Sub items = [
        Title = D
      ]
    ]
  ],
]

我需要接受它并使其看起来像这样:

[
  Title = A
  Sub items = [
    Title = B
    Sub items = [
      Title = C,
      Title = D
    ]
  ]
]

我在下面列出了我的输入和所需的输出。我在这件事上抓狂了。我试图使用递归,但遇到了一些问题,到处都陷入无限循环。任何有关解决此问题的好方法的帮助将不胜感激。

输入:

Array
(
    [0] => Array
        (
            [title] => Dashboard
            [path] => dashboard/
        )

    [1] => Array
        (
            [title] => Settings
            [path] => settings/
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Instances
                            [path] => settings/instances/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Instance
                                            [path] => settings/instances/add-an-instance/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Modules
                            [path] => settings/modules/
                        )

                    [2] => Array
                        (
                            [title] => Administrator
                            [path] => settings/administrator/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Change Password
                                            [path] => settings/administrator/change-password/
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Routes
                            [path] => modules/routes/settings/routes/
                        )

                )

        )

    [3] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Cookies
                            [path] => modules/cookies/settings/cookies/
                        )

                )

        )

    [4] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Sessions
                            [path] => modules/sessions/settings/sessions/
                        )

                )

        )

    [5] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Databases
                            [path] => modules/databases/settings/databases/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a Database
                                            [path] => modules/databases/settings/databases/add-a-database/
                                        )

                                )

                        )

                )

        )

    [6] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Models
                            [path] => modules/models/settings/models/
                        )

                )

        )

    [7] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users and Accounts
                            [path] => modules/users-and-accounts/settings/users-and-accounts/
                        )

                )

        )

    [8] => Array
        (
            [title] => Users and Accounts
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users
                            [path] => modules/users-and-accounts/users/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a User
                                            [path] => modules/users-and-accounts/users/add-a-user/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Accounts
                            [path] => modules/users-and-accounts/accounts/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Account
                                            [path] => modules/users-and-accounts/accounts/add-an-account/
                                        )

                                    [1] => Array
                                        (
                                            [title] => Account Types
                                            [path] => modules/users-and-accounts/accounts/account-types/
                                            [subItems] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [title] => Add an Account Type
                                                            [path] => modules/users-and-accounts/accounts/account-types/add-an-account-type/
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

输出:

Array
(
    [0] => Array
        (
            [title] => Dashboard
            [path] => dashboard/
        )

    [1] => Array
        (
            [title] => Settings
            [path] => settings/
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Instances
                            [path] => settings/instances/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Instance
                                            [path] => settings/instances/add-an-instance/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Modules
                            [path] => settings/modules/
                        )

                    [2] => Array
                        (
                            [title] => Administrator
                            [path] => settings/administrator/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Change Password
                                            [path] => settings/administrator/change-password/
                                        )

                                )

                        )

                    [3] => Array
                    (
                        [title] => Routes
                        [path] => modules/routes/settings/routes/
                    )

                    [4] => Array
                        (
                            [title] => Cookies
                            [path] => modules/cookies/settings/cookies/
                        )

                    [5] => Array
                    (
                        [title] => Sessions
                        [path] => modules/sessions/settings/sessions/
                    )

                    [6] => Array
                    (
                        [title] => Databases
                        [path] => modules/databases/settings/databases/
                        [subItems] => Array
                            (
                                [0] => Array
                                    (
                                        [title] => Add a Database
                                        [path] => modules/databases/settings/databases/add-a-database/
                                    )

                            )

                        )

                    [7] => Array
                    (
                        [title] => Models
                        [path] => modules/models/settings/models/
                    )

                    [8] => Array
                    (
                        [title] => Users and Accounts
                        [path] => modules/users-and-accounts/settings/users-and-accounts/
                    )
                )
        )
    [2] => Array
        (
            [title] => Users and Accounts
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users
                            [path] => modules/users-and-accounts/users/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a User
                                            [path] => modules/users-and-accounts/users/add-a-user/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Accounts
                            [path] => modules/users-and-accounts/accounts/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Account
                                            [path] => modules/users-and-accounts/accounts/add-an-account/
                                        )

                                    [1] => Array
                                        (
                                            [title] => Account Types
                                            [path] => modules/users-and-accounts/accounts/account-types/
                                            [subItems] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [title] => Add an Account Type
                                                            [path] => modules/users-and-accounts/accounts/account-types/add-an-account-type/
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )
)

I'm trying to build a navigation menu. I am receiving an array with a structure like this:

[ 
  [
    Title = A
    Sub items = [
      Title = B
      Sub items = [
        Title = C
      ]
    ]
  ],
  [
    Title = A
    Sub items = [
      Title = B
      Sub items = [
        Title = D
      ]
    ]
  ],
]

I need to take it and make it look like this:

[
  Title = A
  Sub items = [
    Title = B
    Sub items = [
      Title = C,
      Title = D
    ]
  ]
]

I have listed my input and my desired output below. I'm tearing my hair out on this one. I was trying to use recursion, but had some issues with running into infinite loops all over the place. Any help on a good way to tackle this would be so appreciated.

Input:

Array
(
    [0] => Array
        (
            [title] => Dashboard
            [path] => dashboard/
        )

    [1] => Array
        (
            [title] => Settings
            [path] => settings/
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Instances
                            [path] => settings/instances/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Instance
                                            [path] => settings/instances/add-an-instance/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Modules
                            [path] => settings/modules/
                        )

                    [2] => Array
                        (
                            [title] => Administrator
                            [path] => settings/administrator/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Change Password
                                            [path] => settings/administrator/change-password/
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Routes
                            [path] => modules/routes/settings/routes/
                        )

                )

        )

    [3] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Cookies
                            [path] => modules/cookies/settings/cookies/
                        )

                )

        )

    [4] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Sessions
                            [path] => modules/sessions/settings/sessions/
                        )

                )

        )

    [5] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Databases
                            [path] => modules/databases/settings/databases/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a Database
                                            [path] => modules/databases/settings/databases/add-a-database/
                                        )

                                )

                        )

                )

        )

    [6] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Models
                            [path] => modules/models/settings/models/
                        )

                )

        )

    [7] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users and Accounts
                            [path] => modules/users-and-accounts/settings/users-and-accounts/
                        )

                )

        )

    [8] => Array
        (
            [title] => Users and Accounts
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users
                            [path] => modules/users-and-accounts/users/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a User
                                            [path] => modules/users-and-accounts/users/add-a-user/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Accounts
                            [path] => modules/users-and-accounts/accounts/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Account
                                            [path] => modules/users-and-accounts/accounts/add-an-account/
                                        )

                                    [1] => Array
                                        (
                                            [title] => Account Types
                                            [path] => modules/users-and-accounts/accounts/account-types/
                                            [subItems] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [title] => Add an Account Type
                                                            [path] => modules/users-and-accounts/accounts/account-types/add-an-account-type/
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

Output:

Array
(
    [0] => Array
        (
            [title] => Dashboard
            [path] => dashboard/
        )

    [1] => Array
        (
            [title] => Settings
            [path] => settings/
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Instances
                            [path] => settings/instances/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Instance
                                            [path] => settings/instances/add-an-instance/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Modules
                            [path] => settings/modules/
                        )

                    [2] => Array
                        (
                            [title] => Administrator
                            [path] => settings/administrator/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Change Password
                                            [path] => settings/administrator/change-password/
                                        )

                                )

                        )

                    [3] => Array
                    (
                        [title] => Routes
                        [path] => modules/routes/settings/routes/
                    )

                    [4] => Array
                        (
                            [title] => Cookies
                            [path] => modules/cookies/settings/cookies/
                        )

                    [5] => Array
                    (
                        [title] => Sessions
                        [path] => modules/sessions/settings/sessions/
                    )

                    [6] => Array
                    (
                        [title] => Databases
                        [path] => modules/databases/settings/databases/
                        [subItems] => Array
                            (
                                [0] => Array
                                    (
                                        [title] => Add a Database
                                        [path] => modules/databases/settings/databases/add-a-database/
                                    )

                            )

                        )

                    [7] => Array
                    (
                        [title] => Models
                        [path] => modules/models/settings/models/
                    )

                    [8] => Array
                    (
                        [title] => Users and Accounts
                        [path] => modules/users-and-accounts/settings/users-and-accounts/
                    )
                )
        )
    [2] => Array
        (
            [title] => Users and Accounts
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users
                            [path] => modules/users-and-accounts/users/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a User
                                            [path] => modules/users-and-accounts/users/add-a-user/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Accounts
                            [path] => modules/users-and-accounts/accounts/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Account
                                            [path] => modules/users-and-accounts/accounts/add-an-account/
                                        )

                                    [1] => Array
                                        (
                                            [title] => Account Types
                                            [path] => modules/users-and-accounts/accounts/account-types/
                                            [subItems] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [title] => Add an Account Type
                                                            [path] => modules/users-and-accounts/accounts/account-types/add-an-account-type/
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左秋 2024-10-05 19:59:52
<?php

function flattenArray($subitems) {
    $titles = Array() ;
    foreach($subitems as $subkey => $subvalue) {
        if(!is_array($subvalue) 
            || !isset($subvalue['title'])
            || !isset($subvalue['subItems'])
            || !is_array($subvalue['subItems'])) {

            //Leave this element as-is as it doesn't conform to expectations.
            continue ; 
        }
        if(isset($titles[$subvalue['title']])) {
            foreach($subvalue['subItems'] as $subItem) {
                $subitems[$titles[$subvalue['title']]]['subItems'][] = $subItem ;
            }
            unset($subitems[$subkey]) ;
        } else {
            $titles[$subvalue['title']] = $subkey ;
        }
    }
    foreach($subitems as $subkey => $subvalue) {
        if(is_array($subvalue) && isset($subvalue['subItems'])) {
            $subitems[$subkey]['subItems'] = flattenArray($subvalue['subItems']) ;
        }
    }
    return $subitems ;
}
?>
<?php

function flattenArray($subitems) {
    $titles = Array() ;
    foreach($subitems as $subkey => $subvalue) {
        if(!is_array($subvalue) 
            || !isset($subvalue['title'])
            || !isset($subvalue['subItems'])
            || !is_array($subvalue['subItems'])) {

            //Leave this element as-is as it doesn't conform to expectations.
            continue ; 
        }
        if(isset($titles[$subvalue['title']])) {
            foreach($subvalue['subItems'] as $subItem) {
                $subitems[$titles[$subvalue['title']]]['subItems'][] = $subItem ;
            }
            unset($subitems[$subkey]) ;
        } else {
            $titles[$subvalue['title']] = $subkey ;
        }
    }
    foreach($subitems as $subkey => $subvalue) {
        if(is_array($subvalue) && isset($subvalue['subItems'])) {
            $subitems[$subkey]['subItems'] = flattenArray($subvalue['subItems']) ;
        }
    }
    return $subitems ;
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文