开发者

array arithmetic

I have one array as this:

  "4": "40000",
  "5": "3000",
  "6": "200",
  "7": "10",
  "8": "1"

I want to have an another array which adds all the bigger ones to smaller ones. I hope it makes sense. How can I achieve this?

  "4": "43211",
  "5": "3211",
  "6": "211",
  "7": "11",
  "开发者_JAVA百科8": "1"

Just to find an efficient method so language doesn't matter but if necessary I use php or javascript. It is an associative array so it is not sorted. And another trick is that array might or might not contain some elements. So for example "6" or "8" might be missing.


for i = 7 to 4 decreasing
    array [i] += array [i+1]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜