【发布时间】:2014-08-18 13:49:45
【问题描述】:
您好,我收到了如下所示的 JSON 响应。我想计算早于 24 小时的帖子,并检查唯一用户网址:
{
"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"
}
}
]
}
提前致谢。
在@Elias Van Ootegem 的帮助下,我的问题得到了解决。代码如下:
// 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);
【问题讨论】:
-
您能与我们分享您的尝试吗?
-
您的问题与 JSON 无关。只是php多维数组
标签: php arrays json multidimensional-array