1

i have an array like this the sructure of my array, i need to sum "weight" value and push them to the end of array list with key "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
    }]
  }

i want generate summary of "weight" group by key of array like this:

{
  "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
      ]
  }

how to i get generate array like this by sum all of weight value of array? i have try to sum of weight value and push them to the end of list array

2 Answers 2

3

Supporse you array is named $data

    foreach($data as &$array){
        $array["total_weight"] = array_sum(array_column($array,"weight"));
    }
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but might be helpful to include the conversion to a native array, as this will fail on what OP describes as their 'array' (which is literally a JSON string).
1

try with laravel collection collect() then you can use ->sum() function

foreach ($data as $key => $item) {
    $temp = collect($item);
    $data[$key]['total_weight'] = $temp->sum('weight');
}
return $data;

1 Comment

this is easy way to print all of array and get "total_weight"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.