0

Im busy with an chart of my Google Analytics.

I have made a JSON file with the dates and the visitors but now i got of each day of the year and I want to get the visitors each months.

Here is an example of the JSON results:

Dagindex,Gebruikers
1-1-2017,96
2-1-2017,116
3-1-2017,114
4-1-2017,101
5-1-2017,114
6-1-2017,111
7-1-2017,112
8-1-2017,96
9-1-2017,130
10-1-2017,123
11-1-2017,126
12-1-2017,116
13-1-2017,99
14-1-2017,88
15-1-2017,98
16-1-2017,150
17-1-2017,130

PHP Print_r() result:

Array
(
    [0] => Array
        (
            [Dagindex] => 1-1-2017
            [Gebruikers] => 96
        )

    [1] => Array
        (
            [Dagindex] => 2-1-2017
            [Gebruikers] => 116
        )

    [2] => Array
        (
            [Dagindex] => 3-1-2017
            [Gebruikers] => 114
        )

    [3] => Array
        (
            [Dagindex] => 4-1-2017
            [Gebruikers] => 101
        )

    [4] => Array
        (
            [Dagindex] => 5-1-2017
            [Gebruikers] => 114
        )

    [5] => Array
        (
            [Dagindex] => 6-1-2017
            [Gebruikers] => 111
        )

And what i want as result is like this:

01-2017 = XXXXX
02-2017 = XXXXX
03-2017 = XXXXX
till 12-2017 = XXXXX

Now i found this code below on stackoverflow, but i don't know how to change it for my results. Hope someone can help me out.

foreach($yourarray as $key=>$val){
   $result[substr($key,0,7)] += $val;
}
Gurdt
  • 179
  • 16
  • Possible duplicate of [How to parse a date string in PHP?](https://stackoverflow.com/questions/2767324/how-to-parse-a-date-string-in-php) – versvs Apr 30 '18 at 08:24
  • Thanks for the edit, but that still isn't JSON. CSV, maybe? Anyway, it's largely irrelevant, since we already have the data in array format. – ADyson Apr 30 '18 at 08:47

1 Answers1

1

I don't really know if this answer your question, but according to your json and the result you want, I should do more something like that:

$result = array();
foreach($array as $key=>$value){
    list($d,$m,$y) = explode("-", $value['Dagindex']);
    $result[$m.'-'.$y] += $value['Gebruikers'];
}
Fanie Void
  • 331
  • 1
  • 9