0

I may have stupid question but I do not know how to solve my problem.

What I want to achieve.

I need to divide the field by date (key) (specifically, I'm interested in the year) and figure out the total amount (value) for each year.

my array:

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
)

my code:

$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: ' . ???? . ' €';

}

how should I do it?

edit// part of original array

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] => ...

... ...

0

2 Answers 2

4

I would do something like this:

  1. Loop through the whole stuff.
  2. Create another array.
  3. Get the date part and parse the year.
  4. Cumulatively add the years as the key and value as the sum.
  5. Display.

Code:

<?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;
    }
?>

Output

array(7) {
  [2017]=>
  float(0)
  [2016]=>
  float(279800)
  [2015]=>
  float(552003)
  [2014]=>
  float(5307857.33)
  [2013]=>
  float(70000)
  [2012]=>
  float(149880)
  [2011]=>
  float(3252000)
}
Sign up to request clarification or add additional context in comments.

1 Comment

@LubosBelan Good that it works. But: 1. You should parse date using date() and strtotime() method. 2. In lower PHP versions, without initliasing the values, if you try to increment, it might throw an error.
2

Would be way more easier to do something like:

$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);

1 Comment

Notice: Undefined offset: 2017

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.