这是显示每月总销售额的代码
foreach ($truecats as $month => $values)
{
$totdailysale=0;
echo "<tr><td>$month</td>";
foreach ($values as $val)
{
$totdailysale+=$val;
// echo "<td>$totdailysale</td></tr>";
}
echo "<td>$totdailysale</td></tr>";
}
这提供了以下输出:
Date Amount
Aug 175.93
Oct 439.9
Nov 956.98
Dec 1350
Jan 109
这里我得到6个月前的记录。 SEP没有数据,所以我的API没有获得SEP月。但我想展示SEP AS 0 Sale。
答案 0 :(得分:1)
试试这个
<?php
$truecats['Aug'] = array(15, 10);
$truecats['Oct'] = array(12, 10);
$truecats['Nov'] = array(11, 10);
$truecats['Jan'] = array(16, 10);
$arr_months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$start=true;
echo '<table>';
foreach ($truecats as $month => $values)
{
$month_key = array_search($month, $arr_months);
if($start)
{
$key = $month_key;
$start = false;
}
while($key!=$month_key)
{
echo "<tr><td>".$arr_months[$key]."</td>";
echo "<td>0</td></tr>";
$key+=1;
if($key==12)
{
$key=0;
}
}
$totdailysale=0;
echo "<tr><td>$month</td>";
foreach ($values as $val)
{
$totdailysale+=$val;
}
echo "<td>$totdailysale</td></tr>";
$key+=1;
if($key==12)
{
$key=0;
}
}
echo '</table>';
?>
更新2:
如果您想以月Jan
修改上面的代码
if($start)
{
$key = $month_key;
$start = false;
}
带
if($start)
{
$key = 0;
$start = false;
}