Skip to content
Advertisement

How to sum values from associative array if there is the same key in php laravel

Simple code like this:

JavaScript

output:

enter image description here

The result in OCTOBER (10) there is 1 price:

JavaScript

while in NOVEMBER (11) there are 3 prices:

JavaScript

and at DECEMBER (12) is none

I need logic to calculate all prices in months 10, 11, and 12. The expected output is

JavaScript

Thanks 🙂

Advertisement

Answer

I used Collection and end up with the correct results. This could be achieve in a number of ways:

APPROACH 1:

JavaScript

APPROACH 2:

JavaScript

APPROACH 3:

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