开发者

php Array how to iterate

I am using a code-igniter query which returns the the array below with additional calculated [year_totalsales] added to array.

Ar开发者_开发百科ray ( [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 ) [3] => Array ( [year] => 2010 [month] => February [month_sales] => 619 [year_totalsales] => 3610 ) [4] => Array ( [year] => 2010 [month] => March [month_sales] => 732 [year_totalsales] => 3610 ) [5] => Array ( [year] => 2010 [month] => April [month_sales] => 605 [year_totalsales] => 3610 ) )

Is there a way to get the output result to look this:

year-------------------------

month---------------monthsales


well you could do something like:

<?
$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 ));

$linewidth = 29; // Change to the width you want of course
$sep = "-"; // Change the seperator you want
$lastyear = "";
function pad($output, $length) {
    global $sep;
    $output = (string)$output;
    while (strlen($output) < $length) {
        $output .= $sep;
    }
    return $output;
}
foreach($myarray as $row) {
    if ($row['year'] != $lastyear) {
        echo pad($row['year'], $linewidth), PHP_EOL;
        $lastyear = $row['year'];
    }
    echo pad($row['month'], $linewidth-strlen((string)$row['month_sales'])), $row['month_sales'], PHP_EOL;
}
?>

Of course you could add your own formatting or whatever if this isn't satisfactory.

Edit: tested, it works properly now.

Outputs:

2009-------------------------
November------------------524
December------------------521
2010-------------------------
January-------------------609

Edit this uses an HTML table instead:

<?
$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;
?>

This outputs:

<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>


Try this:

foreach($year_array as $months_array)
{
echo 'year        '.$year_array[0];
foreach($months_array as $months)
{
echo 'monthsales        '.$months[0];
}
}


printf could be used quite well to accomplish this. PHP.net docs for sprintf has the details on what the formatting options are.

foreach ($month_data as $m) {
    printf("%d\n%20s $%.2lf\n", $m['year'], $m['month'], $m['month_sales']);
}

If you wanted to only print unique years, as suggested in the comments, you could try this.

$current_year = null;
foreach ($month_data as $m) {
    if ($current_year != $m['year']) {
        printf("%d\n", $m['year']);
        $current_year = $m['year'];
    }
    printf("%20s $%.2lf\n", $m['month'], $m['month_sales']);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜