dousha1394 2018-01-04 01:05
浏览 25
已采纳

计算和打印HTTP API的值

Looking for help in counting all values that are present in HTTP API. The link below outputs the values of multiple data points by date. What I am trying to achieve is a count on each value so that instead of

a:2:{s:10:"2018-01-03";a:9:{s:12:"nb_pageviews";d:2031;}s:10:"2018-01-04";a:9:{s:12:"nb_pageviews";d:25;}

I want to output the above nb_pageviews as a key with the value of the dates combined - 2056.

Example Link for API is https://demo.piwik.org/?module=API&method=Actions.get&idSite=7&period=day&date=last2&format=php&token_auth=anonymous

And the PHP to display this is

$url = "https://demo.piwik.org/";
$url .= "?module=API&method=Actions.get";
$url .= "&idSite=7&period=day&date=last2";
$url .= "&format=php";
$url .= "&token_auth=anonymous";

$fetched = file_get_contents($url);
$content = unserialize($fetched);

// case error
if (!$content) {
   print("NO DATA");
}

foreach ($content as $row) {
   $pageviews = $row['nb_pageviews'];

   print("<div>$pageviews</div>
");       
}

Please note that the above link contains multiple other values that I will be wanting to do the same with, yet for ensuring the readability of this question I have kept the values simple and to just the one.

  • 写回答

1条回答 默认 最新

  • douye9175 2018-01-04 01:13
    关注

    Just use the combined assignment operator, and be sure to set defaults of 0, before the loop.

    $fetched = 'a:2:{s:10:"2018-01-03";a:9:{s:12:"nb_pageviews";d:2031;s:17:"nb_uniq_pageviews";d:964;s:12:"nb_downloads";d:2;s:17:"nb_uniq_downloads";d:2;s:11:"nb_outlinks";d:68;s:16:"nb_uniq_outlinks";d:64;s:11:"nb_searches";d:33;s:11:"nb_keywords";d:16;s:19:"avg_time_generation";d:0.78600000000000003;}s:10:"2018-01-04";a:9:{s:12:"nb_pageviews";d:25;s:17:"nb_uniq_pageviews";d:10;s:12:"nb_downloads";i:0;s:17:"nb_uniq_downloads";i:0;s:11:"nb_outlinks";d:1;s:16:"nb_uniq_outlinks";d:1;s:11:"nb_searches";d:2;s:11:"nb_keywords";d:2;s:19:"avg_time_generation";d:0.79300000000000004;}}';
    
    $content = unserialize($fetched);
    
    // case error
    if (!$content) {
       print("NO DATA");
    }
    
    $pageviews         = 0;
    $uniq_pageviews    = 0;
    // and on, with your other vars you're looking to sum...
    
    foreach ($content as $row) {
       $pageviews      += $row['nb_pageviews'];
       $uniq_pageviews += $row['nb_uniq_pageviews'];
    }
    
    var_export(['pageviews' => $pageviews, 'uniq_pageviews' => $uniq_pageviews]);
    

    Working demo: https://eval.in/930183

    // Output
    array (
        'pageviews' => 2056.0,
        'uniq_pageviews' => 974.0,
    )
    

    To print these values, you might replace the var_export line with something like this:

    $data = [
        'pageviews'      => $pageviews,
        'uniq_pageviews' => $uniq_pageviews
    ];
    

    Which you could output in some HTML like so:

    <div class="col-md-4"><?php echo $data['pageviews']; ?></div>
    <div class="col-md-4"><?php echo $data['uniq_pageviews']; ?></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟