PHP 在保留键的同时遍历多维数组

问题描述 投票:0回答:4

我有一个多维数组,我不知道其深度。例如,该数组可能如下所示:

$array = array(
    1 => array(
        5 => array(
            3 => 'testvalue1'
        )
    ),
    2 => array(
        6 => 'testvalue2'
    ),
    3 => 'testvalue3',
    4 => 'testvalue4',
);

我想用这个数组创建一个目录。这意味着需要保留密钥,因为我将它们用作“章节编号”。例如,“testvalue1”位于第 1.5.3 章中。
现在我想遍历数组,同时保留所有键 - 不使用 array_walk_recursive,因为包含另一个数组的键被删除(正确?),考虑到速度,最好不使用嵌套的 foreach 循环。
有什么建议我应该如何做到这一点?预先感谢。

PS:对于我的脚本,键是字符串(“1”而不是 1)还是整数并不重要,如果使用字符串作为键将使 array_walk_recursive 保留它们。

php arrays loops multidimensional-array
4个回答
9
投票

您可以借助堆栈迭代数组来构建目录。

$stack = &$array;
$separator = '.';
$toc = array();

while ($stack) {
    list($key, $value) = each($stack);
    unset($stack[$key]);
    if (is_array($value)) {
        $build = array($key => ''); # numbering without a title.
        foreach ($value as $subKey => $node)
            $build[$key . $separator . $subKey] = $node;
        $stack = $build + $stack;
        continue;
    }
    $toc[$key] = $key. ' ' . $value;
}

print_r($toc);

输出:

Array
(
    [1] => 1
    [1.5] => 1.5
    [1.5.3] => 1.5.3 testvalue1
    [2] => 2
    [2.6] => 2.6 testvalue2
    [3] => 3 testvalue3
    [4] => 4 testvalue4
)

如果需要,您也可以另外处理该级别,但您的问题并不清楚。

array_walk_recursive
不起作用,因为它不会为您提供父元素的键。另请参阅此相关问题:Transparently flatten an array,它有一个很好的答案,并且对于更通用的情况也很有帮助。


2
投票
<?php
    $td = array(1=>array(5=>array(3=>'testvalue1',array(6=>'testvalue2'))),2=>array(6=>'testvalue2',array(6=>'testvalue2',array(6=>'testvalue2'),array(6=>'testvalue2',array(6=>'testvalue2')))),3=>'testvalue3',4=>'testvalue4');
    print_r($td);
    $toc = '';

    function TOC($arr,$ke='',$l=array()) {
            if (is_array($arr)) {
            if ($ke != '') array_push($l,$ke);
            foreach($arr as $k => $v)
                TOC($v,$k,$l);
        }
        else {
            array_push($l,$ke);
            $GLOBALS['toc'].=implode('.',$l)." = $arr\n";
        }
    }
    toc($td);
    echo "\n\n".$toc;
?>

http://codepad.org/4l4385MZ


0
投票

试试这个:

<?php
$ar = array(
    1 => array(
        5 => array(
            3 => 'testvalue1',
            5 => 'test',
            6 => array(
                9 => 'testval 9'
            )
        ),
        8 => 'testvalue9'
    ),
    2 => array(
        6 => 'testvalue2',
        7 => 'testvalue8',
        2 => array(
            6 => 'testvalue2',
            7 => 'testvalue8'
        ),
    ),
    3 => 'testvalue3',
    4 => 'testvalue4'
);

function getNestedItems($input, $level = array()){
    $output = array();

    foreach($input as $key => $item){
        $level[] = $key;
        if(is_array($item)){
            $output = (array)$output + (array)getNestedItems($item, $level);
        } else {
            $output[(string)implode('.', $level)] = $item;
        }
        array_pop($level);
     }
     return $output;
}

var_dump(getNestedItems($ar));

0
投票

另一种最适合我处理深层嵌套多维关联和非关联数组的方法:

/**
 * Flatten a Multidimensional Array
 */
function array_multidimensional_flatten( $array, $position = [], $separator = '.' ) {
  if ( !is_array( $array ) ) {
    return false;
  }

  $result = [];

  foreach ( $array as $key => $value ) {
    if ( is_array( $value ) ) {
      $position[] = $key;
      $result = array_merge( $result, array_multidimensional_flatten( $value, $position ) );
    } else {
      if ( $position ) {
        $key = implode( $separator, $position ) . "{$separator}{$key}";
      }
      $result = array_merge( $result, [$key => $value] );
    }
  }

  return $result;
}

将其与大型多维数组一起使用将输出如下所示的内容:

[
  'hero__overlay-opacity' => '100',
  'hero__lead-in' => 'Underwritten by TruStage® Insurance',
  'hero__optimize' => '1',
  'hero__layout' => 'centered',
  'flexible-content.0.acf_fc_layout' => 'title',
  'flexible-content.0.acfe_flexible_toggle' => '',
  'flexible-content.0.text' => 'Enabling Smart Financial Decisions',
  'flexible-content.0.size.value' => 'lg',
  'flexible-content.0.size.label' => 'L',
  'flexible-content.0.size.alignment.value' => 'center',
  'flexible-content.0.size.alignment.label' => 'Center',
  'flexible-content.0.size.alignment.weight.value' => 'normal',
  'flexible-content.0.size.alignment.weight.label' => 'Regular',
  'flexible-content.0.1.acf_fc_layout' => 'grid-stats',
  'flexible-content.0.1.acfe_flexible_toggle' => '',
  'flexible-content.0.1.columns.0.statistic' => '85+',
  'flexible-content.0.1.columns.0.description' => 'Years of Experience',
  'flexible-content.0.1.columns.0.1.statistic' => 'A',
  'flexible-content.0.1.columns.0.1.description' => 'Financial rating1',
  'flexible-content.0.1.columns.0.1.2.statistic' => '4.75 / 5',
  'flexible-content.0.1.columns.0.1.2.description' => 'Rating on TrustPilot',
  'flexible-content.0.1.2.acf_fc_layout' => 'icon-columns',
  'flexible-content.0.1.2.acfe_flexible_toggle' => '',
  'flexible-content.0.1.2.title' => 'Insurance Products',
  'flexible-content.0.1.2.background' => 'rgb(246,246,246)',
  'flexible-content.0.1.2.alignment.value' => 'center',
  'flexible-content.0.1.2.alignment.label' => 'Center',
  'flexible-content.0.1.2.alignment.style.value' => 'default',
  'flexible-content.0.1.2.alignment.style.label' => 'Bordered Circle',
  'flexible-content.0.1.2.alignment.style.column.0.icon_type' => 'icon',
  'flexible-content.0.1.2.alignment.style.column.0.icon' => 'insurance-add',
  'flexible-content.0.1.2.alignment.style.column.0.image' => '',
  'flexible-content.0.1.2.alignment.style.column.0.title' => 'AD&D Insurance',
  'flexible-content.0.1.2.alignment.style.column.0.link' => '/insurance/accidental-death-dismemberment/',
]
© www.soinside.com 2019 - 2024. All rights reserved.