我可能有一個愚蠢的問題,但我不知道如何解決我的問題。PHP - 按鍵(日期)選擇值,然後返回值總和
我想達到的目標。
我需要按日期(鍵)(具體來說,我對年度感興趣)劃分字段,並計算出每年的總金額(價值)。
我的數組:
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] => ...
... ...
@LubosBelan好,它的工作原理。但是:1.您應該使用'date()'和'strtotime()'方法解析日期。 2.在較低的PHP版本中,如果您嘗試增加值,則無需啓動值,否則可能會引發錯誤。 –
哦,你說得對 –