我有一个查询SELECT * FROM NavigationMenu ORDER BY parentNavigationMenuId ASC, navigationOrder ASC
将返回一个导航对象数组。我必须使用的结果如下所示:
array (
0 =>
array (
'navigationMenuId' => '1',
'parentNavigationMenuId' => '0',
'fullURL' => '/index',
'contentSlug' => 'index',
'contentAlias' => 'Index',
'navigationAnchor' => 'Home',
'navigationOrder' => '1',
'subNavigationItems' =>
array (
),
),
1 =>
array (
'navigationMenuId' => '2',
'parentNavigationMenuId' => '0',
'fullURL' => '/top-nav-1/index',
'contentSlug' => 'index',
'contentAlias' => 'Index',
'navigationAnchor' => 'Top Nav 1',
'navigationOrder' => '2',
'subNavigationItems' =>
array (
),
),
2 =>
array (
'navigationMenuId' => '3',
'parentNavigationMenuId' => '0',
'fullURL' => '/top-nav-2/index',
'contentSlug' => 'index',
'contentAlias' => 'Index',
'navigationAnchor' => 'Top Nav 2',
'navigationOrder' => '3',
'subNavigationItems' =>
array (
),
),
3 =>
array (
'navigationMenuId' => '8',
'parentNavigationMenuId' => '0',
'fullURL' => '/top-nav-3/index',
'contentSlug' => 'index',
'contentAlias' => 'Top Nav 3',
'navigationAnchor' => 'Top Nav 3',
'navigationOrder' => '4',
'subNavigationItems' =>
array (
),
),
4 =>
array (
'navigationMenuId' => '9',
'parentNavigationMenuId' => '0',
'fullURL' => '/top-nav-4/index',
'contentSlug' => 'index',
'contentAlias' => 'Top Nav 4',
'navigationAnchor' => 'Top Nav 4',
'navigationOrder' => '5',
'subNavigationItems' =>
array (
),
),
5 =>
array (
'navigationMenuId' => '13',
'parentNavigationMenuId' => '0',
'fullURL' => '/top-nav-5/index',
'contentSlug' => 'index',
'contentAlias' => 'Top Nav 5',
'navigationAnchor' => 'Top Nav 5',
'navigationOrder' => '6',
'subNavigationItems' =>
array (
),
),
6 =>
array (
'navigationMenuId' => '4',
'parentNavigationMenuId' => '3',
'fullURL' => '/top-nav-2/sub-nav-1',
'contentSlug' => 'sub-nav-1',
'contentAlias' => 'Sub Nav 1',
'navigationAnchor' => 'Sub Nav 1',
'navigationOrder' => '1',
'subNavigationItems' =>
array (
),
),
7 =>
array (
'navigationMenuId' => '5',
'parentNavigationMenuId' => '3',
'fullURL' => '/top-nav-2/sub-nav-2',
'contentSlug' => 'sub-nav-2',
'contentAlias' => 'Sub Nav 2',
'navigationAnchor' => 'Sub Nav 2',
'navigationOrder' => '2',
'subNavigationItems' =>
array (
),
),
8 =>
array (
'navigationMenuId' => '6',
'parentNavigationMenuId' => '3',
'fullURL' => '/top-nav-2/sub-nav-3',
'contentSlug' => 'sub-nav-3',
'contentAlias' => 'Sub Nav 3',
'navigationAnchor' => 'Sub Nav 3',
'navigationOrder' => '3',
'subNavigationItems' =>
array (
),
),
9 =>
array (
'navigationMenuId' => '7',
'parentNavigationMenuId' => '3',
'fullURL' => '/top-nav-2/sub-nav-4',
'contentSlug' => 'sub-nav-4',
'contentAlias' => 'Sub Nav 4',
'navigationAnchor' => 'Sub Nav 4',
'navigationOrder' => '4',
'subNavigationItems' =>
array (
),
),
10 =>
array (
'navigationMenuId' => '10',
'parentNavigationMenuId' => '9',
'fullURL' => '/top-nav-4/sub-nav-1',
'contentSlug' => 'sub-nav-1',
'contentAlias' => 'Sub Nav 1',
'navigationAnchor' => 'Sub Nav 1',
'navigationOrder' => '1',
'subNavigationItems' =>
array (
),
),
11 =>
array (
'navigationMenuId' => '11',
'parentNavigationMenuId' => '9',
'fullURL' => '/top-nav-4/sub-nav-2',
'contentSlug' => 'sub-nav-2',
'contentAlias' => 'Sub Nav 2',
'navigationAnchor' => 'Sub Nav 2',
'navigationOrder' => '2',
'subNavigationItems' =>
array (
),
),
12 =>
array (
'navigationMenuId' => '12',
'parentNavigationMenuId' => '9',
'fullURL' => '/top-nav-4/sub-nav-3',
'contentSlug' => 'sub-nav-3',
'contentAlias' => 'Sub Nav 3',
'navigationAnchor' => 'Sub Nav 3',
'navigationOrder' => '3',
'subNavigationItems' =>
array (
),
),
)
这些对象中的任何一个都可以有子导航项(一个子项可以有其他子项,依此类推),这就是每个导航项都有一个空数组的原因subNavigationItems
。我基本上想使用navigationMenuId
andparentNavigationMenuId
属性并构建一个 json 对象,该对象具有嵌套在正确父项下的正确子项。我假设它可以通过一系列循环来完成,因为它的逻辑让我头疼。有人对如何实现这一目标有任何想法吗?这是我想要的(或类似的)示例 json:
[
{
"navigationMenuId": "1",
"parentNavigationMenuId": "0",
"fullURL": "/index",
"contentSlug": "index",
"contentAlias": "Index",
"navigationAnchor": "Home",
"navigationOrder": "1",
"subNavigationItems": []
},
{
"navigationMenuId": "2",
"parentNavigationMenuId": "0",
"fullURL": "/top-nav-1/index",
"contentSlug": "index",
"contentAlias": "Index",
"navigationAnchor": "Top Nav 1",
"navigationOrder": "2",
"subNavigationItems": []
},
{
"navigationMenuId": "3",
"parentNavigationMenuId": "0",
"fullURL": "/top-nav-2/index",
"contentSlug": "index",
"contentAlias": "Index",
"navigationAnchor": "Top Nav 2",
"navigationOrder": "3",
"subNavigationItems": [
{
"navigationMenuId": "4",
"parentNavigationMenuId": "3",
"fullURL": "/top-nav-2/sub-nav-1",
"contentSlug": "sub-nav-1",
"contentAlias": "Sub Nav 1",
"navigationAnchor": "Sub Nav 1",
"navigationOrder": "1"
},
{
"navigationMenuId": "5",
"parentNavigationMenuId": "3",
"fullURL": "/top-nav-2/sub-nav-2",
"contentSlug": "sub-nav-2",
"contentAlias": "Sub Nav 2",
"navigationAnchor": "Sub Nav 2",
"navigationOrder": "2"
},
{
"navigationMenuId": "6",
"parentNavigationMenuId": "3",
"fullURL": "/top-nav-2/sub-nav-3",
"contentSlug": "sub-nav-3",
"contentAlias": "Sub Nav 3",
"navigationAnchor": "Sub Nav 3",
"navigationOrder": "3"
},
{
"navigationMenuId": "7",
"parentNavigationMenuId": "3",
"fullURL": "/top-nav-2/sub-nav-4",
"contentSlug": "sub-nav-4",
"contentAlias": "Sub Nav 4",
"navigationAnchor": "Sub Nav 4",
"navigationOrder": "4"
}
]
},
{
"navigationMenuId": "8",
"parentNavigationMenuId": "0",
"fullURL": "/top-nav-3/index",
"contentSlug": "index",
"contentAlias": "Top Nav 3",
"navigationAnchor": "Top Nav 3",
"navigationOrder": "4",
"subNavigationItems": []
},
{
"navigationMenuId": "9",
"parentNavigationMenuId": "0",
"fullURL": "/top-nav-4/index",
"contentSlug": "index",
"contentAlias": "Top Nav 4",
"navigationAnchor": "Top Nav 4",
"navigationOrder": "5",
"subNavigationItems": [
{
"navigationMenuId": "10",
"parentNavigationMenuId": "9",
"fullURL": "/top-nav-4/sub-nav-1",
"contentSlug": "sub-nav-1",
"contentAlias": "Sub Nav 1",
"navigationAnchor": "Sub Nav 1",
"navigationOrder": "1"
},
{
"navigationMenuId": "11",
"parentNavigationMenuId": "9",
"fullURL": "/top-nav-4/sub-nav-2",
"contentSlug": "sub-nav-2",
"contentAlias": "Sub Nav 2",
"navigationAnchor": "Sub Nav 2",
"navigationOrder": "2"
},
{
"navigationMenuId": "12",
"parentNavigationMenuId": "9",
"fullURL": "/top-nav-4/sub-nav-3",
"contentSlug": "sub-nav-3",
"contentAlias": "Sub Nav 3",
"navigationAnchor": "Sub Nav 3",
"navigationOrder": "3"
}
]
},
{
"navigationMenuId": "13",
"parentNavigationMenuId": "0",
"fullURL": "/top-nav-5/index",
"contentSlug": "index",
"contentAlias": "Top Nav 5",
"navigationAnchor": "Top Nav 5",
"navigationOrder": "6",
"subNavigationItems": []
}
]
请让我知道你能想出比我想的更好的方法来处理这个问题。谢谢!