unneccesary陣列當我運行這段代碼:刪除在PHP
$yearRes=DB::table('order')
->select(DB::raw("year(created_at) as y"))
->orderBy("created_at")
->groupBy(DB::raw("year(created_at)"))->get();
foreach ($yearRes as $key => $value) {
$totalOrder[]=DB::table('order')->select(DB::raw("year(created_at) as y,sum(item_price) as p,count(id) as i"))->whereYear('created_at', '=', $value->y)->get();
}
這表明我:
[[{"y":2016,"p":15050,"i":11}],[{"y":2017,"p":8440,"i":3}]]
當我運行這段代碼
$abc=json_encode($totalOrder);
$a=rtrim($abc);
$title=explode('[', $a);
$c=implode('', $title);
$ac=rtrim($c,']');
......結果是這樣的:
{"y":2016,"p":15050,"i":11}],{"y":2017,"p":8440,"i":3}
但我所要的輸出是這樣的:
{"y":2016,"p":15050,"i":11},{"y":2017,"p":8440,"i":3}
什麼是錯我的代碼?
問題不明確,您的預期輸出是什麼? – C2486
你爲什麼認爲這些是「不必要的數組」?你打算如何使用這些數據? – walther