以各种方式计算各种数据

时间:2015-01-14 13:22:07

标签: php amazon-mws

我想要做的是从亚马逊使用MWS api获取订单历史记录,然后按天显示数据作为总结,这是我目前拥有的代码及其输出的内容。

$date = new DateTime();
$dates = [];

for ($i = 0; $i < 30; $i++) {
    $date->sub(new DateInterval('P1D'));
    $dates[] = $date->format('Y-m-d');
}

echo "<head><link href='../../../public/css/bootstrap.css' rel='stylesheet' type='text/css'/></head>";
//echo  "<div class='col-md-12'><div class='col-md-2'>Date</div><div class='col-md-2'>Sales</div><div class='col-md-2'>Amount Shipped</div><div class='col-md-2'>Order Total</div><div class='col-md-2'></div><div class='col-md-2'></div></div>";
foreach ($dates as $key => $value) {
    foreach ($response->getListOrdersResult()->Orders as $order) {
        if (strpos($order->PurchaseDate, $value) !== false) {
            if(substr($order->PurchaseDate, 0, -10) == "$value" ) {
                $array[] = $order->BuyerName;
            }
        echo  "<div class='col-md-12'><div class='col-md-2'>$order->BuyerName</div><div class='col-md-2'>$order->OrderStatus</div><div class='col-md-2'>".$order->OrderTotal->Amount."</div><div class='col-md-2'>".$order->NumberOfItemsShipped."</div><div class='col-md-2'>".substr($order->PurchaseDate, 0, -10)."</div></div>";
        }
    }
}

因此,此代码当前使用MWS api中返回的数据,然后将数据放入按销售日期组合在一起的列中,这就是看起来的样子。

 name    status  amount  date

Somebody Shipped 25.981 2015-01-13
Somebody Shipped 7.971 2015-01-13
Somebody Shipped 14.371 2015-01-13
Somebody Shipped 25.981 2015-01-13
Somebody Shipped 25.981 2015-01-13
Somebody Shipped 29.9712015-01-13
------------------------------------------------------------------2015-01-13
Somebody Shipped 25.981 2015-01-12
Somebody Shipped 35.152 2015-01-12
------------------------------------------------------------------2015-01-12

我想要它做的是计算一个日期有多少记录,例如,第13个有6个,12个有2个,那么我想要添加所有金额,所以这就是我想要的使用相同数据输出的正确版本

 Date       Sales    Amount

2015-1-13 6 130.25

2015-01-12   2        60

实现这一目标的任何帮助都会被挪用,我​​认为我没有留下任何关键信息,因为所有数据都在代码段中

1 个答案:

答案 0 :(得分:1)

只需将值存储到数组中即可。检查循环中的日期与上一个日期相同。如果没有,请将prev日期设置为当前日期,然后创建一个数组键,并添加一个包含当前金额的数组,并将计数器设置为1,如果是,则输入coutner,并将金额添加到之前的金额。当然,$response->getListOrdersResult()->Orders应按日期排序。

$oldDate = 0;
$amounts = array();
foreach ($response->getListOrdersResult()->Orders as $order) {
    if (strpos($order->PurchaseDate, $value) !== false) {
        if (substr($order->PurchaseDate, 0, -10) == "$value") {
            $array[] = $order->BuyerName;
        }
        $date = substr($order->PurchaseDate, 0, -10);
        if ($date !== $oldDate) {
            $oldDate = $date;
            $amounts[$date] = array(
                'amount' => $order->OrderTotal->Amount,
                'cnt' => 1
            );
        } else {
            $amounts[$date]['cnt'] ++;
            $amounts[$date]['amount'] += $order->OrderTotal->Amount;
        }
    }
}
var_dump($amounts);