Michael McCoy Michael McCoy - 1 month ago 4
PHP Question

Splitting array to use with jquery flot

I am building a jquery flot line chart with google analytics api data. I have the analytics working and it is giving me an array like so:

[3] => Array ( [0] => 20160922 [1] => 2217 [2] => 911 ) [4] => Array ( [0] => 20160923 [1] => 2047 [2] => 845 ) [5] => Array ( [0] => 20160924 [1] => 2152 [2] => 924 ) [6] => Array ( [0] => 20160925 [1] => 2502 [2] => 1028 ) [7] => Array ( [0] => 20160926 [1] => 2234 [2] => 877 ) [8] => Array ( [0] => 20160927 [1] => 2020 [2] => 755 ) [9] => Array ( [0] => 20160928 [1] => 1978 [2] => 793 ) [10] => Array ( [0] => 20160929 [1] => 2080 [2] => 867 ) [11] => Array ( [0] => 20160930 [1] => 1632 [2] => 747 ) [12] => Array ( [0] => 20161001 [1] => 1934 [2] => 913 ) [13] => Array ( [0] => 20161002 [1] => 2210 [2] => 1023 ) [14] => Array ( [0] => 20161003 [1] => 2068 [2] => 971 ) [15] => Array ( [0] => 20161004 [1] => 1738 [2] => 918 ) [16] => Array ( [0] => 20161005 [1] => 1787 [2] => 793 ) [17] => Array ( [0] => 20161006 [1] => 1694 [2] => 815 ) [18] => Array ( [0] => 20161007 [1] => 1583 [2] => 813 ) [19] => Array ( [0] => 20161008 [1] => 1906 [2] => 954 ) [20] => Array ( [0] => 20161009 [1] => 1936 [2] => 1047 ) [21] => Array ( [0] => 20161010 [1] => 2188 [2] => 1097 ) [22] => Array ( [0] => 20161011 [1] => 1892 [2] => 938 ) [23] => Array ( [0] => 20161012 [1] => 2036 [2] => 1022 ) [24] => Array ( [0] => 20161013 [1] => 1970 [2] => 915 ) [25] => Array ( [0] => 20161014 [1] => 2044 [2] => 1024 )


I cant seem to figure out how to get this separated out to use in a jquery.flot chart. The above array has a date followed by visits and pageviews that I want to put into a line-chart. All i need help with is the separating of the above array into one for visits and one for pageviews. The rest I can work out. Any help to get me pointed in the right direction will be great!

EDIT

I wanted to give some additional info as I cant seem to get the above array out of the results variable here is what I am using to process the google analytics api:

function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.

$optParams = array(
'dimensions' => 'ga:date'
);
return $analytics->data_ga->get(
'ga:' . $profileId,
'30daysAgo',
'today',
'ga:pageviews, ga:sessions',
$optParams
);
}
//$profile = $r['google_analytics'];
$profile = '69903642';
$results = getResults($analytics, $profile);


I believe the code will work that was given in the first answer but right now it is producing an empty array.

Answer

This code should work for you:

$pageViews = [];
$visits = [];
foreach($data as $key => $item) {
    $pageViews[$key][$item[0]] = $item[2];
    $visits[$key][$item[0]] = $item[1];
}

It will give you two separate arrays. Output:

array(
    0 => array(
        '20160922' => 911
    )
)

array(
    0 => array(
        '20160922' => 2217
    )
)

You can modify the code a little bit:

$pageViews = [];
$visits = [];
foreach($data as $key => $item) {
    $pageViews[$item[0]] = $item[2];
    $visits[$item[0]] = $item[1];
}

for output like this one:

array(
    '20160922' => 2217,
    ......
)
Comments