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 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流