私は私の配列の構造のような配列を持っています、私は「重み」値を合計し、キー「total_weight」でそれらを配列リストの最後にプッシュする必要があります:
{
"730": [{
"qty": "1",
"product_id": 8,
"product_name": "NIKE As M Dry Tee Legend 2.0 - Black\/Black\/Matte Silver [XL]",
"product_price": 299000,
"product_image": "1604906748nike-as-m-dry-tee-legend-20-blackblackmatte-silver-xl.jpg",
"product_slug": "nike-as-m-dry-tee-legend-20-blackblackmatte-silver-xl",
"weight": 100,
"seller_origin": 730
},
{
"qty": "1",
"product_id": 7,
"product_name": "Skipping jump rope Tali Skiping Loncat Speeds Anti Slip 100676 LX",
"product_price": 25000,
"product_image": "1604907088skipping-jump-rope-tali-skiping-loncat-speeds-anti-slip-100676-lx.jpg",
"product_slug": "skipping-jump-rope-tali-skiping-loncat-speeds-anti-slip-lx-1006",
"weight": 200,
"seller_origin": 730
},
{
"qty": 2,
"product_id": 6,
"product_name": "Kappa Track Suit Jacket - Black Black L",
"product_price": 230000,
"product_image": "1602820947kappa-track-suit-jacket-black-black-l.jpg",
"product_slug": "kappa-track-suit-jacket-black-black-l",
"weight": 100,
"seller_origin": 730
}
],
"3641": [{
"qty": "1",
"product_id": 4,
"product_name": "Groot Figure Guardian Pot Marvel",
"product_price": 46999,
"product_image": "1606719651groot-figure-guardian-pot-marvel.jpg",
"product_slug": "groot-figure-guardian-pot-marvel",
"weight": 300,
"seller_origin": 3641
}]
}
このような配列のキーごとに「重み」グループの要約を生成したい:
{
"730": [{
"qty": "1",
"product_id": 8,
"product_name": "NIKE As M Dry Tee Legend 2.0 - Black\/Black\/Matte Silver [XL]",
"product_price": 299000,
"product_image": "1604906748nike-as-m-dry-tee-legend-20-blackblackmatte-silver-xl.jpg",
"product_slug": "nike-as-m-dry-tee-legend-20-blackblackmatte-silver-xl",
"weight": 100,
"seller_origin": 730
},
{
"qty": "1",
"product_id": 7,
"product_name": "Skipping jump rope Tali Skiping Loncat Speeds Anti Slip 100676 LX",
"product_price": 25000,
"product_image": "1604907088skipping-jump-rope-tali-skiping-loncat-speeds-anti-slip-100676-lx.jpg",
"product_slug": "skipping-jump-rope-tali-skiping-loncat-speeds-anti-slip-lx-1006",
"weight": 200,
"seller_origin": 730
},
{
"qty": 2,
"product_id": 6,
"product_name": "Kappa Track Suit Jacket - Black Black L",
"product_price": 230000,
"product_image": "1602820947kappa-track-suit-jacket-black-black-l.jpg",
"product_slug": "kappa-track-suit-jacket-black-black-l",
"weight": 100,
"seller_origin": 730
},
"total_weight":400
],
"3641": [{
"qty": "1",
"product_id": 4,
"product_name": "Groot Figure Guardian Pot Marvel",
"product_price": 46999,
"product_image": "1606719651groot-figure-guardian-pot-marvel.jpg",
"product_slug": "groot-figure-guardian-pot-marvel",
"weight": 300,
"seller_origin": 3641
},
"total_weight":300
]
}
配列の重み値をすべて合計して、このような配列を生成するにはどうすればよいですか?重み値の合計を取得して、リスト配列の最後にプッシュしようとしました
Laravelコレクションを試してみると、関数collect()
を使用できます->sum()
foreach ($data as $key => $item) {
$temp = collect($item);
$data[$key]['total_weight'] = $temp->sum('weight');
}
return $data;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加