php数组如何迭代

时间:2011-05-17 00:53:09

标签: php arrays

我通过codeignitor查询将此数据返回到下面的数组中,并在阵列中添加了额外的计算[year_totalsales]

107815.05   524 2009    November
106716.81   521 2009    December
123851.60   609 2010    January
126752.72   619 2010    February
151362.32   732 2010    March
124616.72   605 2010    April
150702.71   736 2010    May

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

我需要能够将其输入这种格式:

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

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

3 个答案:

答案 0 :(得分:0)

你可以做点什么:

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

当然,如果不满意,您可以添加自己的格式或其他内容。

编辑:已测试,现在可以正常使用。

输出:

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

编辑它会改为使用HTML表格:

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

输出:

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

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

printf可以很好地用来实现这一目标。 PHP.net docs for sprintf详细说明了格式选项。

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

如果您只想按照评论中的建议打印独特年份,可以试试这个。

$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']);
}