Laravel 添加额外数据到数组

我的数据如下:


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

截屏

我的数据库如下所示:

http://img2.mukewang.com/64a9594c0001c6ee08840081.jpg

代码

目前刚刚获得我的专栏,还cart_data需要添加专栏。id


$items2 = CartStorage::where('user_id', $user->id)->get();


$items = [];

foreach($items2 as $item){

  $items[] = json_decode($item['cart_data']); 

}


慕森卡
浏览 143回答 1
1回答

函数式编程

可以通过创建一个 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;}
打开App,查看更多内容
随时随地看视频慕课网APP