dqu92800 2016-10-19 22:14
浏览 43
已采纳

拆分数组与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.

  • 写回答

1条回答 默认 最新

  • douju2474 2016-10-19 22:19
    关注

    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,
        ......
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题