如何在不知道深度的情况下显示多维数组?

时间:2016-02-03 12:50:37

标签: php recursion multidimensional-array foreach treeview

我有一个大的多维数组(我深入数组),我希望以表格形式显示它。这是我的数组中的一个数组

[Kai Roger Tester] => Array
        (
            [Ikke navngitt] => Array
                (
                    [status] => Array
                        (
                            [documents_green] => 0
                            [documents_yellow] => 0
                            [documents_red] => 3
                            [waiting_approval_documents] => 1
                            [waiting_verfication_documents] => 0
                            [under_construction_documents] => 3
                        )

                )

            [Finnfjord] => Array
                (
                    [NVD test] => Array
                        (
                            [status] => Array
                                (
                                    [documents_green] => 0
                                    [documents_yellow] => 0
                                    [documents_red] => 1
                                    [waiting_approval_documents] => 0
                                    [waiting_verfication_documents] => 0
                                    [under_construction_documents] => 5
                                )

                        )

                    [status] => Array
                        (
                            [documents_green] => 0
                            [documents_yellow] => 0
                            [documents_red] => 0
                            [waiting_approval_documents] => 0
                            [waiting_verfication_documents] => 0
                            [under_construction_documents] => 2
                        )

                )

            [Endringslogg] => Array
                (
                    [status] => Array
                        (
                            [documents_green] => 0
                            [documents_yellow] => 0
                            [documents_red] => 0
                            [waiting_approval_documents] => 0
                            [waiting_verfication_documents] => 0
                            [under_construction_documents] => 1
                        )

                )

            [Laste opp doc] => Array
                (
                    [status] => Array
                        (
                            [documents_green] => 0
                            [documents_yellow] => 0
                            [documents_red] => 1
                            [waiting_approval_documents] => 0
                            [waiting_verfication_documents] => 0
                            [under_construction_documents] => 1
                        )

                )

            [status] => Array
                (
                    [documents_green] => 1
                    [documents_yellow] => 0
                    [documents_red] => 6
                    [waiting_approval_documents] => 3
                    [waiting_verfication_documents] => 4
                    [under_construction_documents] => 13
                )

        )
[Prosess 1] => Array
    (
        [AF Decom] => Array
            (
                [status] => Array
                    (
                        [documents_green] => 1
                        [documents_yellow] => 0
                        [documents_red] => 0
                        [waiting_approval_documents] => 0
                        [waiting_verfication_documents] => 0
                        [under_construction_documents] => 3
                    )

            )

        [status] => Array
            (
                [documents_green] => 7
                [documents_yellow] => 0
                [documents_red] => 2
                [waiting_approval_documents] => 0
                [waiting_verfication_documents] => 0
                [under_construction_documents] => 11
            )
    )
Folder Name       green  yellow  red
Kai Roger Tester   1     0     6
  Ikke navngitt'   0     0     3
  Finnfjord        0     0     0
     NVD test      0     0     1
Process1           1     1    0

我厌倦了下面的方法

public static function getfoldertable($array, $prefix = '') {
       $body_start = "<tbody>";
        if (count($array) > 0 && is_array($array)) {
            $i = 0;
            foreach ($array as $key => $row) {

                $body_start.='<tr>
                    <td>'.$i.'</td>
                    <td>'.$key.'</td>
                    <td>'.$row['status']['documents_green'].'</td>
                    <td>'.$row['status']['documents_yellow'].'</td>
                    <td>'.$row['status']['documents_red'].'</td>
                </tr>';

                $body_start.= self::getfoldertable($row, $prefix . '-');
                $i++;
            }
            $body_start.="</tbody>";
        }
        //echo $body_start; die;
        return $body_start;


    }

任何人都可以帮我,我怎么能显示这个?

提前致谢

2 个答案:

答案 0 :(得分:0)

使用RecursiveIterator

工作代码:

$array = array();
$array[0] = array('test' => array('asdf', 'asdf', array('asf', array('asdfads', 'asdf' => array(234,234,234)))));
$iterator = new RecursiveIteratorIterator(
 new RecursiveArrayIterator($array),
 RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $key => $item) {
 if (is_array($item)) {
  echo '<pre>';print_r($item);echo '</pre>';
 }
}

答案 1 :(得分:0)

这是一个可用于生成输出的函数:

function getTreeHTML($tree, $level = 0) {
    $html = "";
    $indent = str_repeat("&nbsp;", $level * 2);
    foreach ($tree as $key => $value) {
        if ($key == "status") continue;
        $html .= "
<tr><td>$indent$key</td>
    <td>{$value['status']['documents_green']}</td>
    <td>{$value['status']['documents_yellow']}</td>
    <td>{$value['status']['documents_red']}</td>
</tr>" . getTreeHTML($value, $level+1);
    }
    return $html;
}

如果输入数据定义如下:

$input = array (
  'Kai Roger Tester' => 
  array (
    'Ikke navngitt' => 
    array (
      'status' => 
      array (
        'documents_green' => 0,
        'documents_yellow' => 0,
        'documents_red' => 3,
        'waiting_approval_documents' => 1,
        'waiting_verfication_documents' => 0,
        'under_construction_documents' => 3,
      ),
    ),
    'Finnfjord' => 
    array (
      'NVD test' => 
      array (
        'status' => 
        array (
          'documents_green' => 0,
          'documents_yellow' => 0,
          'documents_red' => 1,
          'waiting_approval_documents' => 0,
          'waiting_verfication_documents' => 0,
          'under_construction_documents' => 5,
        ),
      ),
      'status' => 
      array (
        'documents_green' => 0,
        'documents_yellow' => 0,
        'documents_red' => 0,
        'waiting_approval_documents' => 0,
        'waiting_verfication_documents' => 0,
        'under_construction_documents' => 2,
      ),
    ),
    'Endringslogg' => 
    array (
      'status' => 
      array (
        'documents_green' => 0,
        'documents_yellow' => 0,
        'documents_red' => 0,
        'waiting_approval_documents' => 0,
        'waiting_verfication_documents' => 0,
        'under_construction_documents' => 1,
      ),
    ),
    'Laste opp doc' => 
    array (
      'status' => 
      array (
        'documents_green' => 0,
        'documents_yellow' => 0,
        'documents_red' => 1,
        'waiting_approval_documents' => 0,
        'waiting_verfication_documents' => 0,
        'under_construction_documents' => 1,
      ),
    ),
    'status' => 
    array (
      'documents_green' => 1,
      'documents_yellow' => 0,
      'documents_red' => 6,
      'waiting_approval_documents' => 3,
      'waiting_verfication_documents' => 4,
      'under_construction_documents' => 13,
    ),
  ),
  'Prosess 1' => 
  array (
    'AF Decom' => 
    array (
      'status' => 
      array (
        'documents_green' => 1,
        'documents_yellow' => 0,
        'documents_red' => 0,
        'waiting_approval_documents' => 0,
        'waiting_verfication_documents' => 0,
        'under_construction_documents' => 3,
      ),
    ),
    'status' => 
    array (
      'documents_green' => 7,
      'documents_yellow' => 0,
      'documents_red' => 2,
      'waiting_approval_documents' => 0,
      'waiting_verfication_documents' => 0,
      'under_construction_documents' => 11,
    ),
  ),
);

...你会像这样调用这个函数:

$html = getTreeHTML($input);

...在表格中嵌入HTML结果如下:

<table border=1>
  <tr><th>Folder Name</th><th>green</th><th>yellow</th><th>red</th></tr>
  <?=$html?>
</table>

...然后输出将在这样的浏览器中查找:

enter image description here