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;
}
2
  • Possible duplicate of How to parse a date string in PHP? Commented Apr 30, 2018 at 8: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. Commented Apr 30, 2018 at 8:47

1 Answer 1

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'];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Also try this, its a short code but i get an "Notice: Undefined index: 1-2017 in"
hum, I forgot to add a security if the index is not existing... add a if (!isset($result[$m.'-'.$y])) { $result[$m.'-'.$y] = 0; } before the $result[...] = ....
How can i add this :)
You just have to add it between the lines with list() and $result

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.