PHP - 按键选择值(日期),然后返回值sum

时间:2017-08-22 16:59:02

标签: php arrays

我可能有愚蠢的问题,但我不知道如何解决我的问题。

我想要实现的目标。

我需要按日期(关键)划分字段(具体来说,我对这一年感兴趣)并计算出每年的总金额(值)。

我的阵列:

Array
(
    [2017-04-20 15:26:08] => 0.00
    [2016-10-11 13:52:05] => 0.00
    [2016-05-26 13:04:14] => 200000.00
    [2016-05-24 14:32:34] => 0.00
    [2016-05-24 11:32:17] => 0.00
    [2016-03-29 19:02:24] => 79800.00
    [2016-02-16 07:54:38] => 0.00
    [2015-12-16 19:01:55] => 149900.00
    [2015-01-30 15:07:38] => 402103.00
    [2014-11-03 15:13:29] => 0.00
    [2014-10-15 15:58:44] => 129000.00
    [2014-10-06 12:44:52] => 0.00
    [2014-07-28 10:47:45] => 0.00
    [2014-07-23 15:50:24] => 133333.33
    [2014-05-16 12:39:25] => 0.00
    [2014-04-29 13:21:01] => 5045524.00
    [2014-03-04 15:28:03] => 0.00
    [2014-01-13 09:41:47] => 0.00
    [2013-11-26 08:40:20] => 70000.00
    [2013-07-22 12:17:24] => 0.00
    [2012-10-05 13:51:06] => 0.00
    [2012-03-08 15:45:44] => 149880.00
    [2012-03-02 13:11:19] => 0.00
    [2012-02-14 09:26:43] => 0.00
    [2011-12-05 10:44:23] => 0.00
    [2011-12-05 10:33:53] => 1500000.00
    [2011-07-01 14:40:22] => 0.00
    [2011-05-30 09:54:22] => 1680000.00
    [2011-01-13 08:59:14] => 72000.00
)

我的代码:

$min_year = date('Y',strtotime(min(array_column($MY_ARRAY,'published_at'))));
$max_year = date('Y',strtotime(max(array_column($MY_ARRAY,'published_at'))));

for($year = $max_year; $year >= $min_year; $year--){
$amount_array = array_column($MY_ARRAY,'contract_amount','published_at');

echo 'Year: ' .$year . ', Total amount for this year: ' . ???? . ' €';

}

我应该怎么做?

编辑//原始数组的一部分

Array
(
    [0] => stdClass Object
        (
            [id] => 2898917
            [contract_identifier] => 155/AD00/2017
            [contracting_authority_name] => some company
            [contracting_authority_formatted_address] => address
            [contracting_authority_cin] => 123456789
            [supplier_name] => another company
            [supplier_formatted_address] => 
            [supplier_cin] => n123456789mber
            [subject] => contract title
            [subject_description] => 
            [signed_on] => 2017-04-19
            [effective_note] => 
            [contract_price_amount] => 0.00
            [contract_price_total_amount] => 0.00
            [note] => 
            [published_at] => 2017-04-20 15:26:08
            [changed_at] => 2017-04-20 15:10:09
            [change_note] => 
            [internal_id] => 155
            [internal_note] => 
            [confirmation_file_name] => 
            [confirmed_on] => 
            [source_id] => 2
            [description] => 
            [reference] => 
            [effective_from] => 2017-04-21
            [effective_to] => 
            [crz_department_name] => department
            [crz_status_name] => 1
            [crz_type_name] => 1
            [crz_kind_name] => 2
            [crz_confirmation_status_name] => 5
            [crz_attachments_title] => 11111
            [crz_attachments_file_name] => 
            [crz_attachments_file_size] => 
            [crz_attachments_scan_file_name] => 2898918_dokument.pdf
            [crz_attachments_scan_file_size] => 373463
        )

    [1] => stdClass Object
        (
            [id] => 2635819
            [contract_identifier] => 241
            [contracting_authority_name] => ...

... ...

2 个答案:

答案 0 :(得分:4)

我会做这样的事情:

  1. 遍历整个内容。
  2. 创建另一个数组。
  3. 获取日期部分并解析年份。
  4. 累计将年份作为关键和值作为总和。
  5. 显示。
  6. 代码:

    <?php
        $arr = array(
            "2017-04-20 15:26:08" => 0.00,
            "2016-10-11 13:52:05" => 0.00,
            "2016-05-26 13:04:14" => 200000.00,
            "2016-05-24 14:32:34" => 0.00,
            "2016-05-24 11:32:17" => 0.00,
            "2016-03-29 19:02:24" => 79800.00,
            "2016-02-16 07:54:38" => 0.00,
            "2015-12-16 19:01:55" => 149900.00,
            "2015-01-30 15:07:38" => 402103.00,
            "2014-11-03 15:13:29" => 0.00,
            "2014-10-15 15:58:44" => 129000.00,
            "2014-10-06 12:44:52" => 0.00,
            "2014-07-28 10:47:45" => 0.00,
            "2014-07-23 15:50:24" => 133333.33,
            "2014-05-16 12:39:25" => 0.00,
            "2014-04-29 13:21:01" => 5045524.00,
            "2014-03-04 15:28:03" => 0.00,
            "2014-01-13 09:41:47" => 0.00,
            "2013-11-26 08:40:20" => 70000.00,
            "2013-07-22 12:17:24" => 0.00,
            "2012-10-05 13:51:06" => 0.00,
            "2012-03-08 15:45:44" => 149880.00,
            "2012-03-02 13:11:19" => 0.00,
            "2012-02-14 09:26:43" => 0.00,
            "2011-12-05 10:44:23" => 0.00,
            "2011-12-05 10:33:53" => 1500000.00,
            "2011-07-01 14:40:22" => 0.00,
            "2011-05-30 09:54:22" => 1680000.00,
            "2011-01-13 08:59:14" => 72000.00
        );
        $sum = array();
        foreach ($arr as $year => $value) {
            $year = date('Y', strtotime($year));
            if (!isset($sum[$year]))
                $sum[$year] = 0.0;
            $sum[$year] += $value;
        }
    ?>
    

    <强>输出

    array(7) {
      [2017]=>
      float(0)
      [2016]=>
      float(279800)
      [2015]=>
      float(552003)
      [2014]=>
      float(5307857.33)
      [2013]=>
      float(70000)
      [2012]=>
      float(149880)
      [2011]=>
      float(3252000)
    }
    

答案 1 :(得分:2)

更容易做出类似的事情:

$sum = array();
foreach ($MY_ARRAY as $date => $value) {
    $year = substr($date, 0, 4);
    $sum[$year] = (isset($sum[$year]) ? $sum[$year] : 0) + $value;
}
print_r($sum);