Skip to content
Advertisement

How to calculate two data in one array?

I’m a beginner in php, I want to calculate two data from one array, but I still don’t understand how. For example, I have one data array

JavaScript

and I want the output like this

JavaScript

I try some code but didn’t help, and now I got stuck.

sorry my language is not good.

Advertisement

Answer

Just use two loops to iterate over the data:

JavaScript

For bigger data sets you might want to optimize it. Faster but harder to read:

JavaScript

The output obviously is:

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