3

私は多次元配列を持っており、array_values().

ただし、subNavigationItemsサブアレイのインデックスも再作成する必要があります。

これは私が現在受け取っているものですarray_values($arr)

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 であり、2 番目のインデックスが 10、11、および 12 であることに注意してください。両方とも 0 から開始する必要があります。 do this は無限再帰関数にすることができます。

すべての単一のアイテムに、subNavigationItemsより多くのもので埋められる可能性のある配列があり、それらの配列も 0 で再インデックスする必要があります。

これを達成する方法について何か考えはありますか?

4

3 に答える 3

0

私はこれがそれを行うべきだと思います:

function reindex_array_recursive($array) {
    if (is_array($array)) {
        if (array_keys($array) === range(0, count($array) - 1)) { // Indexed array
            return array_values(array_map('reindex_array_recursive', $array));
        } else { // Associative array
            foreach ($array as $value) {
                $value = reindex_array_recursively($value);
            }
            return $array;
        }
    } else {
        return $array;
    }
}

オブジェクトには再帰せず、サブ配列のみに再帰します。

于 2013-11-01T16:41:44.987 に答える