0

i've got an array of dates as keys and values (integers) in the form:

[2015-07-14] => 40
[2015-07-15] => 5
[2015-07-16] => 8
[2015-07-17] => 0
[2015-07-18] => 0
[2015-07-19] => 0
[2015-07-20] => 0
[2015-07-21] => 0
[2015-07-22] => 0
[2015-07-23] => 0
[2015-07-24] => 0
[2015-07-25] => 0
[2015-07-26] => 0
[2015-07-27] => 0
[2015-07-28] => 0
[2015-07-29] => 0
[2015-07-30] => 0
[2015-07-31] => 0
[2015-08-01] => 0
[2015-08-02] => 1
[2015-08-03] => 1
[2015-08-04] => 2
[2015-08-05] => 1

The startdate and enddate can be selected by the user.

Is there a quick and easy way to combine those dates and sum the values as per month? In my example, the result should look somethine like:

[2015-07] => 53
[2015-08] => 5

The way that I tried to solve that was to use explode functions and then try to recombine those, but that seems to me a bit more complicated than it needs to be.

4
  • What have you tried so far? We need to see what you've tried to tell you if it works; but the answer is likely to be subjective and as such this question is likely to be flagged for closure. Commented Sep 9, 2015 at 13:16
  • 1
    So quick question, if I was to select the starting point from 2015-07-22 to 2015-07-31 will I get a result of 0? Or will the days before 2015-07-22 count as days in the month, even though I haven't selected that day? Or better question can a user select days too or just months? Commented Sep 9, 2015 at 13:20
  • 1
    A combination of array_map and array_sum would be a solution Commented Sep 9, 2015 at 13:24
  • People answering with substr I just wanna point out it's a really bad idea as date formatting may change in the future. You'd want to stick with the DateTime for this one. Commented Sep 9, 2015 at 13:26

3 Answers 3

2

something like

foreach($yourarray as $key=>$val){
   $result[substr($key,0,7)] += $val;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. That works like a charm. I knew there was an easier way than using explode("-",...) :-)
Note that this will throw E_NOTICE errors if they are turned on (and they should).
yes, it will throw notice, you can use @ to ignore it or just simply define each one, but PHP can easily handle theese ones, it wont harm your code read this question please: stackoverflow.com/questions/8132410/… its bad practice only because it would kill your code in another language but NOT in PHP
0

Sure there is.

$per_month = array();
foreach ($arr as $key => $val) {
  $month = substr($key, 0, 7);
  if (!isset($per_month[$month])) {
    $per_month[$month] = 0;
  }
  $per_month[$month] += $val;
}
echo var_dump($per_month["2015-07"]);

Edit:

Also, instead of substr() you could use sscanf()

foreach...
list($y, $m, $d) = sscanf($key, "%d-%d-%d");
$per_month[$y.'-'.$m] += $val;
endforeach

... or explode()

foreach...
$var = explode('-', $key); // $var[0] = year, $var[1] = month ...
...
$per_month[$var[0].'-'.$var[1]] += $val;
endforeach

There are probably many more ways, these are just off the top of my head

1 Comment

Thank you Alex, I was using the explode-function befor, but I thought this makes things more complicated than they need to be.
0

For me substr() method didn't worked in Laravel.

So I tried it differently instead of substr() method.

$monthwise_data = array();
foreach($datewise_data as $key => $value){
    $month_key = date('Y-m', strtotime($key));
    if(array_key_exists($month_key, $monthwise_data)){
        // If we've already added this month to the new array, add the value
        $monthwise_data[$month_key] += $value;
    }
    else{
        // Otherwise create a new element with month as key and store the value
        $monthwise_data[$month_key] = $value;
    }
}
dd($monthwise_data);

Comments

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.