-1

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);
3
  • 3
    Can you share with us what you have tried? Commented Aug 18, 2014 at 13:51
  • possible duplicate of Working with JSON and a multidimensional array (JS) Commented Aug 18, 2014 at 13:54
  • Your question isn't related to JSON. Just php multidimensional arrays Commented Aug 18, 2014 at 13:58

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

with that i got a array (size=0) empty
@fabu_baracus: In which case, there are no posts since yesterday
when i try using your example the loop dosen't seem to loop. is it possible that i have to use two loops inside each other, because of the multidimensional array???
@fabu_baracus: If the response truly looks like the example you show here, then no: the array of posts is found in $jsonDecodedData->posts, each element of this array is an instance of stdClass, and its timestamp is found under the property posted. Note that the data you posted here lists posts that are older than 1 day (08/16 is 2 days ago)
if you're using the code I posted here, that should've been json_decode($jsonData), without the second parameter (true), and the function is called json_decode, not jsondecode... you can keep decoding the data to an array, but replace the property accessing bits by array indexes (foreach ($data['posts'] as $post) instead of $data->posts). Also ensure the variable containing the json string is actually called $jsonData
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.