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,第二个索引是 10、11 和 12。我需要它们都从 0 开始。如果函数这样做可以是一个无限递归函数。

如果您看到每个项目都有一个subNavigationItems可能会填充更多东西的数组,那么我也需要将这些数组重新索引为 0。

关于我如何做到这一点的任何想法?

4

3 回答 3

4

用这个

<?php 

    function reOrderArray($array) {
        if(! is_array($array)) {
             return $array;
        }
        $count = 0;
        $result = array();
        foreach($array as $k => $v) {
            if(is_integer_value($k)) {
               $result[$count] = reOrderArray($v);
               ++$count;
            } else {
              $result[$k] = reOrderArray($v);
            }
        }
        return $result;
    }

    public function is_integer_value($value) {
        if(!is_int($value)) {
            if(is_string($value) && preg_match("/^-?\d+$/i",$value)) {
                return true;
            }
            return false;
        }
        return true;
    }
于 2013-11-01T16:46:05.720 回答
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 回答
-1

子数组可预测地放置在subNavigationItems键控元素中,因此无需检查每个级别中每个元素的数据类型。

仅当它不为空array_values()时才需要调用重新索引键的递归调用。subNavigationItems

与其他答案相比,以下内容明显更干净,功能开销更少。

代码:(演示

function reindexChildren(array $array): array {
    foreach ($array as &$item) {
        if ($item['subNavigationItems']) {
            $item = reindexChildren($item['subNavigationItems']);
        }
    }
    return array_values($array);
}

var_export(
    reindexChildren($menu)
);
于 2020-10-05T10:59:54.483 回答