我的數據如下:data: [,…] 0: {name: "Product One", price: "15000", quantity: 2, attributes: {attr: {name: "weight", value: "35"}},…} attributes: {attr: {name: "weight", value: "35"}} conditions: [{name: "blue", value: 0}, {name: "L", value: 0}] name: "Product One" price: "15000" quantity: 2我需要添加id到每個數據數組中,例如:data: [,…] 0: {name: "Product One", price: "15000", quantity: 2, attributes: {attr: {name: "weight", value: "35"}},…} attributes: {attr: {name: "weight", value: "35"}} conditions: [{name: "blue", value: 0}, {name: "L", value: 0}] name: "Product One" price: "15000" quantity: 2 id: 10 // added id截屏我的數據庫如下所示:代碼目前剛剛獲得我的專欄,還cart_data需要添加專欄。id$items2 = CartStorage::where('user_id', $user->id)->get();$items = [];foreach($items2 as $item){ $items[] = json_decode($item['cart_data']); }
1 回答

函數式編程
TA貢獻1807條經驗 獲得超9個贊
可以通過創建一個 var 并將其添加到那里,如下所示:
$items2 = CartStorage::where('user_id', $user->id)->get();
$items = [];
foreach($items2 as $item){
$decodedItems = json_decode($item['cart_data'], true);
$decodedItems['id'] = $item['id'];
$items[] = $decodedItems;
}
- 1 回答
- 0 關注
- 164 瀏覽
添加回答
舉報
0/150
提交
取消