1 回答

TA貢獻1828條經驗 獲得超13個贊
有點斗志旺盛,但這應該可以了。$chart 變量將保存您要查找的內容。
// Assuming data is the "array for object in this format"
$dates = array_reduce($data, function ($dates, $record) {
if (!in_array($record['o_date']), $dates) {
$dates[] = $record['o_date'];
}
return $dates;
}, []);
$stores = array_reduce($data, function ($stores, $record) {
if (!in_array($record['storeName'], $stores)) {
$stores[] = $record['storeName'];
}
return $stores;
}, []);
$headers = $stores;
$headers[] = [ 'role' => 'annotation' ])
$chart = [ $headers ];
foreach ($dates as $date) {
$datapoint = [ $date ];
foreach ($stores as $store) {
$total = 0;
foreach ($data as $record) {
$isStore = $record['storeName'] === $store;
$isDate = $record['o_date'] === $date;
if($isStore && $isDate) {
$total += (float) $record['total_amount'];
}
}
$datapoint[] = $total;
}
$chart[] = $datapoint;
}
- 1 回答
- 0 關注
- 138 瀏覽
添加回答
舉報