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 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开