每月同一周的值之和

时间:2016-09-26 13:13:01

标签: php

我有一个像这样的数组: -

application: not a procedure;
 expected a procedure that can be applied to arguments
  given: 3
  arguments...: [none]
  errortrace...:
   /home/xiaolong/development/LISP/Racket/SICP/exercise-2.04-procedural-representation-of-pairs.rkt:24:6: (p)
   /home/xiaolong/development/LISP/Racket/SICP/exercise-2.04-procedural-representation-of-pairs.rkt:42:0: (car x)
  context...:
   /home/xiaolong/development/LISP/Racket/SICP/exercise-2.04-procedural-representation-of-pairs.rkt: [running body]

我想添加同一周的价值。欲望输出: -

int steps = 5;
int tmp = 0;
for (int j = 0; j < imageUrl.size(); j++) {
    if (imageUrl.size() < steps) {
        for (int i = 0; i < imageUrl.size(); i++) {
            buildPicture(imageUrl.get(tmp));
            imageUrl.remove(tmp);
            tmp++;
        }
    } else if (imageUrl.size() >= steps) {
        for (int i = 0; i < steps; i++) {
            buildPicture(imageUrl.get(tmp));
            imageUrl.remove(tmp);
            tmp++;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您应该使用与kw连接的年份作为密钥,并使用相同的密钥对值进行求和。

$dates = array
(
    '2016-09-26' => 3,
    '2016-09-24' => 1,
    '2016-09-23' => 2,
    '2016-09-22' => 1,
    '2016-09-21' => 3
);

$result = array();
foreach ($dates as $date => $value){
    $key = intval(date("Y", strtotime($date)).date("W", strtotime($date)));

    if (!isset($result[$key])){
        $result[$key] = $value;
    } else{
        $result[$key]+= $value;
    }
}
rsort($result);

print_r($result);