多维数组列表打印Php

q3aa0525  于 2023-05-27  发布在  PHP
关注(0)|答案(3)|浏览(129)

我有以下数组与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>
)
kxxlusnw

kxxlusnw1#

你可以这样做:

<?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);

?>

输出:

rdrgkggo

rdrgkggo2#

好的使用这个功能它的工作

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

ctehm74n3#

对于$var[] =,您基本上是说您想要添加一个新的Element到$var中,并带有一个递增的键。递归函数返回一个数组。所以这个数组被赋值为数组中的一个新元素。但你想要的是一个平面阵列。
而不是像这样向数组中添加数组:

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

Merge现有数组如下所示:

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

相关问题