Skip to content
Advertisement

sum value in foreach loop based on another value php

I have an array like below. There are id, label, cost, and cid in an array. We want to sum the cost based on the cid like for cid 22 cost should be 196.5 and for cid 11 cost should be 44.4. In our out put array we want to keep the label, cost (sum), and cid, Please see expected output array.

JavaScript

Tried below

JavaScript

Getting below as an output

JavaScript

Want expected output like below.

JavaScript

Basically want to sum cost based on cid and want to keep the label, cost (sum), and cid.

Any help will be greatly appreciated.

Advertisement

Answer

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement