Array help plz

Hi,

I have the following code so far :

[code]<?
$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 “

”, PHP_EOL;
foreach($myarray as $row) {
if ($row[‘year’] != $lastyear) {
echo “”, PHP_EOL;
$lastyear = $row[‘year’];
}
echo “”, PHP_EOL;
}
echo “
Month Sales
”, $row[‘year’], “
”, $row[‘month’], “ ”, $row[‘month_sales’], “
”, PHP_EOL;
?>
[/code]

BUt this code gives me this:

[code]

Month Sales
2009
November 524
December 521
2010
January 609
[/code]

Instead of this:

[CODE]

Month Sales
2009
November 524
December 521
Month Sales
2010
January 609
[/CODE]

ANy help appreciated thx

Revised code with the desired result :

[php]<?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 = “”;

foreach($myarray as $row) {

if ($row['year'] != $lastyear) {
	echo "<table><tr><th>Month</th><th>Sales</th></tr>", PHP_EOL;
    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 “”, PHP_EOL;
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service