kiwimediapro Posted May 19, 2011 Share Posted May 19, 2011 Hi, I have the following code so far : <? $myarray = array( 0 => array( 'year' => 2009, 'month' => "November", 'month_sales' => 524, 'year_totalsales' => 3610 ), 1 => array ( 'year' => 2009, 'month' => 'December', 'month_sales' => 521, 'year_totalsales' => 3610 ), 2 => array ( 'year' => 2010, 'month' => "January", 'month_sales' => 609, 'year_totalsales' => 3610 )); $lastyear = ""; echo "<table><tr><th>Month</th><th>Sales</th></tr>", PHP_EOL; foreach($myarray as $row) { if ($row['year'] != $lastyear) { echo "<tr><th colspan=2>", $row['year'], "</th></tr>", PHP_EOL; $lastyear = $row['year']; } echo "<tr><td>", $row['month'], "</td><td>", $row['month_sales'], "</td></tr>", PHP_EOL; } echo "</table>", PHP_EOL; ?> BUt this code gives me this: <table><tr><th>Month</th><th>Sales</th></tr> <tr><th colspan=2>2009</th></tr> <tr><td>November</td><td>524</td></tr> <tr><td>December</td><td>521</td></tr> <tr><th colspan=2>2010</th></tr> <tr><td>January</td><td>609</td></tr> </table> Instead of this: <table><tr><th>Month</th><th>Sales</th></tr> <tr><th colspan=2>2009</th></tr> <tr><td>November</td><td>524</td></tr> <tr><td>December</td><td>521</td></tr> </table> <table><tr><th>Month</th><th>Sales</th></tr> <tr><th colspan=2>2010</th></tr> <tr><td>January</td><td>609</td></tr> </table> ANy help appreciated thx Quote Link to comment https://forums.phpfreaks.com/topic/236913-array-help-lz/ Share on other sites More sharing options...
jcbones Posted May 19, 2011 Share Posted May 19, 2011 <?php $myarray = array( 0 => array( 'year' => 2009, 'month' => "November", 'month_sales' => 524, 'year_totalsales' => 3610 ), 1 => array ( 'year' => 2009, 'month' => 'December', 'month_sales' => 521, 'year_totalsales' => 3610 ), 2 => array ( 'year' => 2010, 'month' => "January", 'month_sales' => 609, 'year_totalsales' => 3610 )); $lastyear = NULL; foreach($myarray as $row) { if ($row['year'] != $lastyear) { echo (!empty($lastyear)) ? '</table>', PHP_EOL : NULL; echo "<table><tr><th>Month</th><th>Sales</th></tr>", PHP_EOL,"<tr><th colspan=2>", $row['year'], "</th></tr>", PHP_EOL; $lastyear = $row['year']; } echo "<tr><td>", $row['month'], "</td><td>", $row['month_sales'], "</td></tr>", PHP_EOL; } echo "</table>", PHP_EOL; ?> Quote Link to comment https://forums.phpfreaks.com/topic/236913-array-help-lz/#findComment-1217832 Share on other sites More sharing options...
kiwimediapro Posted May 19, 2011 Author Share Posted May 19, 2011 Woohoo! I htink youve done it. Teting and let you know. Quote Link to comment https://forums.phpfreaks.com/topic/236913-array-help-lz/#findComment-1217854 Share on other sites More sharing options...
kiwimediapro Posted May 20, 2011 Author Share Posted May 20, 2011 YEs it works! All hail the PHP guru . ...... :-) Using the same array code i need to create a javascript array for Google Charts Like this: raw_data=[['May',1429,0],['June',1565,0],['July',1581,0],['August',1437,0],['September',627,0],['October',660,0],['November',524,0],['December',521,0],['January',0,609],['February',0,619],['March',0,732],['April',0,605]]; Where: raw_data= [ [ month1 , year1 sales figure(zero if no figure), year2 sales figure(zero if no figure).....], [ month1 , year1 sales figure(zero if no figure), year2 sales figure(zero if no figure).....]......]; Quote Link to comment https://forums.phpfreaks.com/topic/236913-array-help-lz/#findComment-1217860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.