dongmaqiu6084 2014-08-18 13:49
浏览 42

JSON响应多维数组

Hello i got a JSON response that looks like the one below. I want to count the posts that are younger then 24 hours and also check for unique user urls:

{  
   "meta":{  
      "network":"all",
      "query_type":"realtime"
   },
   "posts":[  
      {  
         "network":"facebook",
         "posted":"2014-08-16 08:31:31 +00000",
         "sentiment":"neutral",
         "url":"someURL",
         "user":{  
            "name":"Terance Podolski",
            "url":"someURL",
            "image":"someURL"
         }
      },
      {  
         "network":"facebook",
         "posted":"2014-08-16 08:30:44 +00000",
         "sentiment":"neutral",
         "url":"someURL",
         "user":{  
            "name":"Łukasz Podolski",
            "url":"someURL",
            "image":"someURL"
         }
      },
      {  
         "network":"facebook",
         "posted":"2014-08-16 08:25:39 +00000",
         "sentiment":"neutral",
         "url":"someURL",
         "user":{  
            "name":"Marcin Podolski",
            "url":"someURL",
            "image":"someURL"
         }
      }
]
}

Thanks in advance.

With the help of @Elias Van Ootegem i got my problem solved. The code looks like that:

// Json Reponse decodieren
$jsonArray = json_decode($jsonData);



function getMentionsFromLast24H($myArray){
    // set variable exactly one day ago
    $since = new DateTime('-1 day');

    // array where to store timestamps in
    $recent = array();

    foreach ( $myArray -> posts as $post ) {

        try {
            $post -> posted = new DateTime (substr ( $post->posted,0,19 ) );//create DateTime instance
            if ( $post -> posted >= $since )
                $recent[] = $post;//add to array
        } catch ( Exception $e ) {
            echo $e -> getMessage();
            exit(1);
        }

    }

    return $recent;

}

$mentions24h = count(getMentionsFromLast24H($jsonArray));

print_r($mentions24h);
  • 写回答

1条回答 默认 最新

  • drhwb470572 2014-08-18 13:57
    关注

    It's pretty simple, really: decode the json data, compare the posted values with time - 24 hours, if the value is great than time-24 hours, add it to an array. That's it, you'll end up with an array containing all posts that were added in the last 24 hours:

    $data = json_decode($jsonData);//creates object
    $since = new DateTime('yesterday');
    $recent = array();//this is the array we'll be constructing
    foreach ($data->posts as $post)
    {
        $post->posted = new DateTime($post->posted);//create DateTime instance
        if ($post->posted > $since)
            $recent[] = $post;//add to array
    }
    var_dump($recent);//this is the array you're after
    

    That really is all there is to it.

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能