php多维数组显示

时间:2017-06-15 08:36:42

标签: php arrays multidimensional-array

我有一个多维数组,其数据结构如下所示,如何迭代以获得所需的输出

Array
(
    [P1-VVS2-VVS2] => Array
        (
            [S] => Array
                (
                    [1] => Array
                        (
                            [key] => P1-VVS2-VVS2
                            [saw] => S
                            [row_num] => 1
                            [assign] => 0
                            [total_dollar] => 1490140.75
                            [stone_weight] => 24.5
                            [dollar] => 4963.00
                        )

                )

            [C] => Array
                (
                    [1] => Array
                        (
                            [key] => P1-VVS2-VVS2
                            [saw] => C
                            [row_num] => 1
                            [assign] => 0
                            [total_dollar] => 6080976
                            [stone_weight] => 44
                            [dollar] => 6282.00
                        )

                )

        )

)

所需的输出方式,如何使用每个循环或任何其他来显示所需的结果数据

    P1-VVS2-VVS2
    S
    1
    0
    1490140.75
    24.5
    4963.00


    P1-VVS2-VVS2
    C
    1
    0
    6080976
    44
    6282.00

4 个答案:

答案 0 :(得分:2)

foreach($array as $key=>$item){
    echo $key."<br>";
    foreach($item as $subKey=>$subItem){
        echo $subKey."<br>";;
        foreach($subItem as $value){
            echo $value."<br>";
        }
    }
}

答案 1 :(得分:1)

您可以使用array_walk_recursive,如果您还想输出中间空间,请参阅此post以获取extendd array_walk_recursive。

array_walk_recursive($array, function($v){
  echo $v . "\n";

});

答案 2 :(得分:1)

你可以试试这个

foreach ($array as $arr){

    array_walk_recursive($arr, function($res){
        echo $res . "\n". '<br>';
    });
}

答案 3 :(得分:0)

<?php

$marray = array
    (
    'P1 - VVS2 - VVS2' => Array
        (
        'S' => Array
            (
            '1' => Array
                (
                'key' => 'P1 - VVS2 - VVS2',
                'saw' => S,
                'row_num' => 1,
                'assign' => 0,
                'total_dollar' => 1490140.75,
                'stone_weight' => 24.5,
                'dollar' => '4963.00'
            )
        ),
        'C' => Array
            (
            '1' => Array
                (
                'key' => 'P1 - VVS2 - VVS2',
                'saw' => C,
                'row_num' => 1,
                'assign' => 0,
                'total_dollar' => 6080976,
                'stone_weight' => 44,
                'dollar' => '6282.00'
            )
        )
    )
);




foreach ($marray as $key => $item) {
    foreach ($item as $sub_item) {
        foreach ($sub_item as $sub_item1) {
            foreach ($sub_item1 as $sub_item2) {
                print $sub_item2 . '<br>';

            }
            echo '<br><br>';
        }
    }
}

?>