在foreach循环中打印动态索引多维数组并在表中打印

时间:2017-10-09 06:20:50

标签: php arrays loops multidimensional-array foreach

我有多个索引数组,这里索引动态变化。实际上数组的索引是group的id。如何将这些数组传递给foreach循环

 Array
(
    [0] => Array
        (
            [462] => Array
                (
                    [0] => Array
                        (
                            [group_name] => First
                            [invoice_no] => 

                        )

                    [1] => Array
                        (
                            [invoice_no] => 
                            [invoice_no] => 2Q

                        )

                )

            [525] => Array
                (
                    [0] => Array
                        (
                            [group_name] => Second
                            [invoice_no] => 

                        )

                    [1] => Array
                        (
                            [group_name] => 
                            [invoice_no] => 3QW

                        )

                )


        )

)

这是我的数组结构,请帮我在php中使用foreach循环打印结果,不改变数组结构 我正在尝试这段代码

foreach ($Sale_list_array as $key => $value) {
    echo ($key);

    echo $value[$key][group_name];
}

但是,这不能解决我的问题。我希望在表结构

中输出这样的输出
<table>
<thead>
<tr>
<td>Group</td>
<td>Invoice</td>
</tr>
</thead>
<tbody>
<tr><td>First</td><td></td></tr>
<tr><td></td><td>2Q</td></tr>
<tr><td>Second</td><td></td></tr>
<tr><td></td><td>3QW</td></tr>
</tbody>
</table>

2 个答案:

答案 0 :(得分:1)

试试这个

$newArray = array();
foreach ($Sale_list_array as $key => $value) {
   $newArray[$key] = $value[$key][group_name];
}
print_r($newArray)

答案 1 :(得分:1)

尝试下面的代码,$wholeArr是变量,它保存您的完整数组值

     foreach( $wholeArr as $group ){
        foreach( $group as $groupKey => $groupValues ){
           foreach( $groupValues as $groupVal ){
             echo $groupVal["group_name"]."<br/>";
           }
         }
       }