Skip to content
Advertisement

php function to sum values from multi dimensional array

My $results array looks like this..

JavaScript

I want to echo the combined sum for each individual item (total_value, total_stickers, total_orders) for each “marketplace” by date and thought i could do this if i pass the variables in a function and tried the following..

JavaScript

That way I can change the variables and sum any of them with a similar echo line but this is not working for me, do I also need to specify dates in a foreach? or how can I achieve this expected output..

JavaScript

Advertisement

Answer

As always, no need to over-engineer, if you know the structure, just iterate trough the values, and sum them up, this is how I would do that.

This way you can add many other marketplace and dates without later modifying the code.

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