多维数组列表打印Php

时间:2014-04-28 10:33:07

标签: php arrays recursion multidimensional-array

我有以下数组与php代码 我找不到我错的地方

我试图过滤其中一些数组结果并删除它们。当我尝试列出它们时,我无法成功

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',
          4 => 'kabeduvarkad.jpg',
          5 => 'kabeduvarkad1-1000x288.jpg',
          6 => 'kabeduvarkad1-1024x768.jpg',
          7 => 'kabeduvarkad1-150x150.jpg',
          8 => 'kabeduvarkad1-300x225.jpg',
          9 => 'kabeduvarkad1-400x300.jpg',
          10 => 'kabeduvarkad1.jpg',
          11 => 'kabeduvarkad2-1000x288.jpg',
          12 => 'kabeduvarkad2-1024x768.jpg',
          13 => 'kabeduvarkad2-150x150.jpg',
          14 => 'kabeduvarkad2-300x225.jpg',
          15 => 'kabeduvarkad2-400x300.jpg',
          16 => 'kabeduvarkad2.jpg',
        ),
        10 => 
        array (
        ),
      ),
      2014 => 
      array (
        '02' => 
        array (
        ),
      ),
    ),
  ),
  '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',
)

这是我的功能

function listArrayRecursive($array_name, $ident = ''){
    $result = array();
    foreach ($array_name as $k => $v){
        if (is_array($v)){
            $result[] = listArrayRecursive($v, $ident.'/'.$k);
        }else{
            $result[] = $ident. '/' . $v . '<br>';
        }
    }
    return $result;
}

我有以下结果

Array
(
    [0] => /do-update.php<br>
    [1] => /sitemap.xml<br>
    [2] => /sitemap.xml.gz<br>
    [3] => /wp-config.php<br>
    [4] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
                                    [1] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
                                    [2] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
                                    [3] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
                                    [4] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
                                    [5] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
                                    [6] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
                                    [7] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
                                    [8] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
                                    [9] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
                                    [10] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
                                    [11] => /wp-content/uploads/2013/05/kabeduvarkad2-1000x288.jpg<br>
                                    [12] => /wp-content/uploads/2013/05/kabeduvarkad2-1024x768.jpg<br>
                                    [13] => /wp-content/uploads/2013/05/kabeduvarkad2-150x150.jpg<br>
                                    [14] => /wp-content/uploads/2013/05/kabeduvarkad2-300x225.jpg<br>
                                    [15] => /wp-content/uploads/2013/05/kabeduvarkad2-400x300.jpg<br>
                                    [16] => /wp-content/uploads/2013/05/kabeduvarkad2.jpg<br>
                                )

                            [1] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [0] => Array
                                (
                                )

                        )

                )

        )

    [5] => Array
        (
            [0] => /wp-update/wp-update.tar<br>
            [1] => /wp-update/wp-update.tar.gz<br>
            [2] => /wp-update/wp-update1.tar<br>
            [3] => /wp-update/wp-update1.tar.gz<br>
        )

    [6] => /wp-update.tar.gz<br>
)

预期结果

Array
(
    [0] => /do-update.php<br>
    [1] => /sitemap.xml<br>
    [2] => /sitemap.xml.gz<br>
    [3] => /wp-config.php<br>
    [4] => /wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg<br>
    [5] => /wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg<br>
    [6] => /wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>
    [7] => /wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg<br>
    [8] => /wp-content/uploads/2013/05/kabeduvarkad.jpg<br>
    [9] => /wp-content/uploads/2013/05/kabeduvarkad1-1000x288.jpg<br>
    [10] => /wp-content/uploads/2013/05/kabeduvarkad1-1024x768.jpg<br>
    [11] => /wp-content/uploads/2013/05/kabeduvarkad1-150x150.jpg<br>
    [12] => /wp-content/uploads/2013/05/kabeduvarkad1-300x225.jpg<br>
    [13] => /wp-content/uploads/2013/05/kabeduvarkad1-400x300.jpg<br>
    [14] => /wp-content/uploads/2013/05/kabeduvarkad1.jpg<br>
    ...
    [110] => /wp-update/wp-update.tar<br>
    [111] => /wp-update/wp-update.tar.gz<br>
    [112] => /wp-update/wp-update1.tar<br>
    [113] => /wp-update/wp-update1.tar.gz<br>
    [114] => /wp-update.tar.gz<br>
)

3 个答案:

答案 0 :(得分:0)

确定使用此功能正常工作

$all=array();
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); 
 foreach($it as $v) { 
   $all[]=$v;
 } 
print_r($all);

答案 1 :(得分:0)

你可以这样做:

<?php

// Dummy data source
$data = array(
    '/do-update.php',
    '/sitemap.xml',
    '/sitemap.xml.gz',
    '/wp-config.php',
    array(
        array(
            array(
                '/wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
                '/wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
                '/wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg<br>'
            )
        )
    )
);

// Helper function
function getFiles($data, &$fileList) {
    foreach ($data as $dataItem) {
        if (is_array($dataItem))
            getFiles($dataItem, $fileList);
        else
            $fileList[] = $dataItem;
    }
}

// Debug
echo "<b>Orignal Array</b>";
var_dump($data);
echo "<hr>";

// Helper function usage
echo "<b>Parsed Array</b>";
$fileList = array();
getFiles($data, $fileList);
var_dump($fileList);

?>

输出:

enter image description here

答案 2 :(得分:-2)

使用$var[] =,您基本上可以说要使用递增的密钥向$var添加新元素。 你的Recursive frunction返回一个数组。 因此,此数组被指定为数组中的新元素。 但你想要的是一个平面阵列。

而不是像这样在数组中添加数组:

if (is_array($v)){
    $result[] = listArrayRecursive($v, $ident.'/'.$k);

Merge现有数组如下:

if (is_array($v)){
    $tmpResult = listArrayRecursive($v, $ident.'/'.$k);
    $result    = array_merge($result, $tmpResult);

您可以看到一个有效的例子here