php - JSON Response Multidimensional Array -
hello got json response looks 1 below. want count posts younger 24 hours , check 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 of @elias van ootegem got problem solved. code looks that:
// json reponse decodieren $jsonarray = json_decode($jsondata); function getmentionsfromlast24h($myarray){ // set variable 1 day ago $since = new datetime('-1 day'); // array store timestamps in $recent = array(); foreach ( $myarray -> posts $post ) { try { $post -> posted = new datetime (substr ( $post->posted,0,19 ) );//create datetime instance if ( $post -> posted >= $since ) $recent[] = $post;//add array } catch ( exception $e ) { echo $e -> getmessage(); exit(1); } } return $recent; } $mentions24h = count(getmentionsfromlast24h($jsonarray)); print_r($mentions24h);
it's pretty simple, really: decode json data, compare posted
values time - 24 hours, if value great time-24 hours, add array. that's it, you'll end array containing posts added in last 24 hours:
$data = json_decode($jsondata);//creates object $since = new datetime('yesterday'); $recent = array();//this array we'll constructing foreach ($data->posts $post) { $post->posted = new datetime($post->posted);//create datetime instance if ($post->posted > $since) $recent[] = $post;//add array } var_dump($recent);//this array you're after
that there it.
Comments
Post a Comment