演练整个多维数组PHP

时间:2014-05-01 17:30:57

标签: php arrays recursion multidimensional-array iteration

我有一个虚拟数组,我想订购。

我如何获得以下结果

与foreach一起? 与下一个() 使用RecursiveIterator 使用IteratorIterator

哪一个最快?

这是数组

$files = array (
  0 => 'do-update.php',
  1 => 'sitemap.xml',
  2 => 'sitemap.xml.gz',
  3 => 'wp-config.php',
  'wp-content' => 
  array (
    'uploads' => 
    array (
      2013 => 
      array (
        '05' => 
        array (
          0 => 'kabeduvarkad-1024x768.jpg',
          1 => 'kabeduvarkad-150x150.jpg',
          2 => 'kabeduvarkad-300x225.jpg',
          3 => 'kabeduvarkad-940x198.jpg',
        ),
        10 => 
        array (
        ),
      ),
      2014 => 
      array (
        '02' => 
        array (
        ),
      ),
      2015 => 'de.php',
    ),
  ),
  'wp-update' => 
  array (
    0 => 'wp-update.tar',
    1 => 'wp-update.tar.gz',
    2 => 'wp-update1.tar',
    3 => 'wp-update1.tar.gz',
  ),
  4 => 'wp-update.tar.gz',
);

预期结果

$expected = array (
  0 => 'do-update.php',
  1 => 'sitemap.xml',
  2 => 'sitemap.xml.gz',
  3 => 'test.php',
  4 => 'wp-config.php',
  5 => 'wp-content/',
  6 => 'wp-content/uploads/',
  7 => 'wp-content/uploads/2013/',
  8 => 'wp-content/uploads/2013/05/',
  9 => 'wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
  10 => 'wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
  11 => 'wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg',
  12 => 'wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg',
  13 => 'wp-content/uploads/2013/05/kabeduvarkad.jpg',
  14 => '...'
);

1 个答案:

答案 0 :(得分:0)

array_walk_recursive正是您要找的。

示例(未测试):

$expected = [];

array_walk_recursive($your_array, function($item, $key) {
    // push item on to $expected
    $expected[] = $item;
});

Working example