我已經尋找了一段時間了一個解決方案,但似乎無法得到一個工作。我有一個多維數組,並且能夠成功地使用array_values()
將重新索引爲0的「父」數組項目(它們被索引爲1,JSON將它當作一個對象而不是數組對待)。但是,嵌套數組需要從0重新索引,我似乎無法得到它的工作。這就是我目前正在與array_values($arr)
接收:PHP - 獲得array_values重新索引嵌套數組
array (
0 =>
array (
'navigationMenuId' => '1',
'parentNavigationMenuId' => '0',
'contentId' => '1',
'fullURL' => '/index',
'contentSlug' => 'index',
'contentAlias' => 'Index',
'navigationAnchor' => 'Home',
'navigationOrder' => '1',
'subNavigationItems' =>
array (
),
),
1 =>
array (
'navigationMenuId' => '2',
'parentNavigationMenuId' => '0',
'contentId' => '15',
'fullURL' => '/top-nav-1/index',
'contentSlug' => 'index',
'contentAlias' => 'Index',
'navigationAnchor' => 'Top Nav 1',
'navigationOrder' => '2',
'subNavigationItems' =>
array (
),
),
2 =>
array (
'navigationMenuId' => '3',
'parentNavigationMenuId' => '0',
'contentId' => '7',
'fullURL' => '/top-nav-2/index',
'contentSlug' => 'index',
'contentAlias' => 'Index',
'navigationAnchor' => 'Top Nav 2',
'navigationOrder' => '3',
'subNavigationItems' =>
array (
4 =>
array (
'navigationMenuId' => '4',
'parentNavigationMenuId' => '3',
'contentId' => '8',
'fullURL' => '/top-nav-2/sub-nav-1',
'contentSlug' => 'sub-nav-1',
'contentAlias' => 'Sub Nav 1',
'navigationAnchor' => 'Sub Nav 1',
'navigationOrder' => '1',
'subNavigationItems' =>
array (
),
),
5 =>
array (
'navigationMenuId' => '5',
'parentNavigationMenuId' => '3',
'contentId' => '10',
'fullURL' => '/top-nav-2/sub-nav-2',
'contentSlug' => 'sub-nav-2',
'contentAlias' => 'Sub Nav 2',
'navigationAnchor' => 'Sub Nav 2',
'navigationOrder' => '2',
'subNavigationItems' =>
array (
),
),
6 =>
array (
'navigationMenuId' => '6',
'parentNavigationMenuId' => '3',
'contentId' => '9',
'fullURL' => '/top-nav-2/sub-nav-3',
'contentSlug' => 'sub-nav-3',
'contentAlias' => 'Sub Nav 3',
'navigationAnchor' => 'Sub Nav 3',
'navigationOrder' => '3',
'subNavigationItems' =>
array (
),
),
7 =>
array (
'navigationMenuId' => '7',
'parentNavigationMenuId' => '3',
'contentId' => '11',
'fullURL' => '/top-nav-2/sub-nav-4',
'contentSlug' => 'sub-nav-4',
'contentAlias' => 'Sub Nav 4',
'navigationAnchor' => 'Sub Nav 4',
'navigationOrder' => '4',
'subNavigationItems' =>
array (
),
),
),
),
3 =>
array (
'navigationMenuId' => '8',
'parentNavigationMenuId' => '0',
'contentId' => '5',
'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',
'contentId' => '6',
'fullURL' => '/top-nav-4/index',
'contentSlug' => 'index',
'contentAlias' => 'Top Nav 4',
'navigationAnchor' => 'Top Nav 4',
'navigationOrder' => '5',
'subNavigationItems' =>
array (
10 =>
array (
'navigationMenuId' => '10',
'parentNavigationMenuId' => '9',
'contentId' => '12',
'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',
'contentId' => '13',
'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',
'contentId' => '14',
'fullURL' => '/top-nav-4/sub-nav-3',
'contentSlug' => 'sub-nav-3',
'contentAlias' => 'Sub Nav 3',
'navigationAnchor' => 'Sub Nav 3',
'navigationOrder' => '3',
'subNavigationItems' =>
array (
),
),
),
),
5 =>
array (
'navigationMenuId' => '13',
'parentNavigationMenuId' => '0',
'contentId' => '4',
'fullURL' => '/top-nav-5/index',
'contentSlug' => 'index',
'contentAlias' => 'Top Nav 5',
'navigationAnchor' => 'Top Nav 5',
'navigationOrder' => '6',
'subNavigationItems' =>
array (
),
),
)
通知第一嵌套數組索引如何4,5,6,和7,和第二個的索引是10,11,和12我需要它們都從0開始。如果這樣做的函數可以是無限遞歸函數,那也是非常好的。如果你看到每一個項目都有一個subNavigationItems
數組,可能會被更多的東西填充,我也需要這些數組重新索引爲0。
我如何能做到這一點有什麼想法?
謝謝!這正是我想要的。 – ryanpitts1