0

我有一个像这样的数组:var_export():

数组 ( 2009 => 数组 ('Jan' => 数组 ('AMT' => '79', 'QTY' => '886', ), 'Apr' => 数组 ('AMT' => '0', 'QTY' => '1', ), 'May' => 数组 ('AMT' => '19', 'QTY' => '216', ), 'Jun' => 数组 ('AMT' => '5', 'QTY' => '60', ), 'Sep' => 数组 ('AMT' => '20', 'QTY' => '196', ), 'Oct' => 数组 (' AMT' => '0', 'QTY' => '1', ), 'Feb' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Mar' => 数组 (' AMT' => 0, 'QTY' => 0, ), 'Jul' => 数组 ('AMT' => 0, 'QTY' => 0,), 'Aug' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Nov' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Dec ' => 数组 ('AMT' => 0, 'QTY' => 0, ), ), 2010 => 数组 ('Mar' => 数组 ('AMT' => '12', 'QTY' => ' 131', ), 'Apr' => 数组 ('AMT' => '0', 'QTY' => '1', ), 'Jun' => 数组 ('AMT' => '6', 'QTY ' => '65', ), 'Jul' => 数组 ('AMT' => '24', 'QTY' => '205', ), 'Aug' => 数组 ('AMT' => '20 ', 'QTY' => '169', ), 'Sep' => 数组 ('AMT' => '0', 'QTY' => '1', ),'Oct' => 数组 ('AMT' => '106', 'QTY' => '1118', ), 'Nov' => 数组 ('AMT' => '85', 'QTY' => '902 ', ), 'Dec' => 数组 ('AMT' => '0', 'QTY' => '2', ), 'Jan' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Feb' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'May' => 数组 ('AMT' => 0, 'QTY' => 0, ), ), 2011 => 数组 ('Mar' => 数组 ('AMT' => '0', 'QTY' => '3', ), 'Sep' => 数组 ('AMT' => '74', 'QTY' => '582', ), 'Oct' => 数组 ('AMT' => '6', 'QTY' => '60', ),'Nov' => 数组 ('AMT' => '46', 'QTY' => '462', ), 'Jan' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Feb' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Apr' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'May' = > 数组 ('AMT' => 0, 'QTY' => 0, ), 'Jun' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Jul' => 数组 (' AMT' => 0, 'QTY' => 0, ), 'Aug' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Dec' => 数组 ('AMT' => 0, '数量' => 0, ), ), )数组 ('AMT' => 0, 'QTY' => 0, ), 'Feb' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Apr' => 数组 ('AMT ' => 0, 'QTY' => 0, ), 'May' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Jun' => 数组 ('AMT' => 0 , 'QTY' => 0, ), 'Jul' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Aug' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Dec' => 数组 ('AMT' => 0, 'QTY' => 0, ), ), )数组 ('AMT' => 0, 'QTY' => 0, ), 'Feb' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Apr' => 数组 ('AMT ' => 0, 'QTY' => 0, ), 'May' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Jun' => 数组 ('AMT' => 0 , 'QTY' => 0, ), 'Jul' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Aug' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Dec' => 数组 ('AMT' => 0, 'QTY' => 0, ), ), )=> 0, 'QTY' => 0, ), 'Jun' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Jul' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Aug' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Dec' => 数组 ('AMT' => 0, 'QTY' = > 0, ), ), )=> 0, 'QTY' => 0, ), 'Jun' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Jul' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Aug' => 数组 ('AMT' => 0, 'QTY' => 0, ), 'Dec' => 数组 ('AMT' => 0, 'QTY' = > 0, ), ), )

我一直在使用一个只有一个嵌套数组的数组,并在这样的表中显示我的数据:

foreach($results as $key => $element){
    echo "<tr>";
    foreach($element as $subkey => $subelement){
        echo "<td width='20%'>$subelement</td>";
    }
    echo "</tr>";
}

但是,现在我有 Year (2009,2010,2011) 并且不知道如何将它放入我的表中。在我刚刚显示 Month、Amt、Qty 之前......因此是上面的 foreach。

我想像这样显示它:

年份 | 月 | 安特 | 数量

我试图在 if 语句中捕捉年份,if(2009){show $subelement} 类型的想法......

4

1 回答 1

2

您可能需要一个外部循环来循环这些年。假设您已经构建了表头,它可能如下所示:

foreach($results as $year => $year_data) {
    foreach ($year_data as $month => $month_data) {
        echo "<tr><td>$year</td><td>$month</td><td>{$month_data['AMT']}</td><td>{$month_data['QTY']}</td></tr>";
    }
}
于 2013-02-06T00:31:19.693 回答