0
votes

I have an array like so: var_export():

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

I have been using an array with only a single nested array and displaying my data in a table like this:

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

However, now I have Year (2009,2010,2011) and am not sure how to get it into my table. Before I was just showing Month, Amt, Qty... hence the foreach above.

I'd like to display it like so:

Year | Month | Amt | Qty

I was trying to capture the year in an if statement, if(2009){show $subelement} type of idea...

1
can you post the output of var_export() instead of var_dump() (or print_r()) ?hek2mgl
@hek2mgl edited the post with the var_export() data. Thanksjasonflaherty

1 Answers

2
votes

You will probably need an outer loop to loop through the years. Assuming you have your table header already built, it could look like this:

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>";
    }
}