Tuesday, 15 June 2010

Foreach loop into table from array data php -



Foreach loop into table from array data php -

i have array 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 array single nested array , displaying info in table this:

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

however, have year (2009,2010,2011) , not sure how table. before showing month, amt, qty... hence foreach above.

i'd display so:

year | month | amt | qty

i trying capture year in if statement, if(2009){show $subelement} type of idea...

you need outer loop loop through years. assuming have table header built, this:

foreach($results $year => $year_data) { foreach ($year_data $month => $month_data) { echo "<tr><td>$year</td><td>$month</td><td>{$month_data['amt']}</td><td>{$month_data['qty']}</td></tr>"; } }

php multidimensional-array foreach

No comments:

Post a Comment