使用递归来展平带有基于值的子项的 PHP 数组(为此浪费了一个小时!)
我正在尝试构建一个导航菜单。我收到一个具有如下结构的数组:
[
[
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)