大家好,我有一个问题。我正在做一个查询,并希望得到记录组的一天。现在我已经完成了这段代码:
$pedidosChart = DB::table('pedidos')
->selectRaw(' count(*) as count , DATE_FORMAT(fecha_creado, '%d') as day , sum( pedidos.precio_total) as amount ')
->whereMonth('fecha_creado', 12)
->groupBy('day')
->get()
i got this:
0 => {#1388 ▼
+"count": 4
+"day": "15"
+"amount": 6073.5798950195
}
1 => {#1387 ▼
+"count": 8
+"day": "17"
+"amount": 22565.599243164
}
2 => {#1389 ▼
+"count": 11
+"day": "18"
+"amount": 13111.219726562
}
and I want getting:
[▼
15 => array:2 [▼
"count" => 4
"amount" => 6073.5798950195
]
17 => array:2 [▼
"count" => 8
"amount" => 22565.599243164
]
18 => array:2 [▼
"count" => 11
"amount" => 13111.219726562
]
21 => array:2 [▼
"count" => 4
"amount" => 2910.5600585938
]
我得到了上面的结果,因为使用foreach(循环)来排序数据。可能吗?如何?
如果您不想从实际数组中删除day
键,则有一个快速解决方案。
您可以直接使用laravel collection方法groupBy。
$grouped = $pedidosChart->groupBy('day');
另一种解决方案是使用foreach循环并将key设置为day
的值,然后取消设置。
$grouped = [];
foreach($pedidosChart as $key => $obj) {
$grouped[$obj->day] = $obj;
unset($grouped[$obj->day]['day']);
}