-1

I have a decoded json array which I received from the facebook API:

$Myposts = json_decode($response->getBody());

When I printed $Myposts :

var_dump($Myposts);

It gived this :

    object(stdClass)[16]
  public 'data' => 
    array (size=24)
      0 => 
        object(stdClass)[12]
          public 'message' => string 'a mesagge..' (length=65)
          public 'created_time' => string '2016-09-18T16:41:10+0000' (length=24)
          public 'id' => string '111110037' (length=35)
      1 => 
        object(stdClass)[28]
          public 'message' => string 'How brave !' (length=11)
          public 'story' => string 'XXX shared Le XX video.' (length=59)
          public 'created_time' => string '2016-09-18T13:37:33+0000' (length=24)
          public 'id' => string '102172976' (length=35)

      23 => 
        object(stdClass)[50]
          public 'message' => string '...a message..' (length=259)
          public 'story' => string 'Bi added 3 new photos.' (length=33)
          public 'created_time' => string '2015-12-11T20:54:21+0000' (length=24)
          public 'id' => string '102191588' (length=35)
  public 'paging' => 
    object(stdClass)[51]
      public 'previous' => string 'https://graph.facebook.com/v2.7/XXXX&__paging_token=YYYY&__previous=1' (length=372)
      public 'next' => string 'https://graph.facebook.com/v2.7/XXX/feed?access_token=DDDD&limit=25&until=XXX&__paging_token=XXXX' (length=362)

I'm new to php, and I don't have any idea how to deal with this output, I would like to loop through all the messages and output the created_time of each message.

Any ideas ?

EDIT : I tried : echo $myPosts['data'][message]; from Parsing JSON file with PHP, but I have had : "Undefined index: message". That's why I posted a new question.

Community
  • 1
  • 1
Bill
  • 315
  • 3
  • 18
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/4343596/parsing-json-file-with-php – Marcs Sep 27 '16 at 07:38
  • if you want to use these values than use loop. – devpro Sep 27 '16 at 07:41
  • I tried to use loop, but I didn't figured out how to extract the data array. I used thid : `foreach ($aposts['data'] as $item) { echo "items:". $item['message'] ."\n"; }` but got an error. – Bill Sep 27 '16 at 07:46

1 Answers1

1

The second parameter of json_decode turns the result into an associative array:

$myPosts = json_decode($response->getBody(), true);

foreach ($myPosts['data'] as $post) {
    var_dump($post['message']);
}

Without using the second parameter the decoding returns objects:

$myPosts = json_decode($response->getBody());

foreach ($myPosts->data as $post) {
    var_dump($post->message);
}
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • I tested the first one, it works! but I don't know why I'm getting this error: `Notice: Undefined index: message in C:\wamp64\www\ctest.php on line 131` For the second one, I got this one : `Notice: Undefined property: stdClass::$message in C:\wamp64\www\ctest.php on line 138` – Bill Sep 27 '16 at 07:58
  • Maybe not all the posts have a message.. you should check if the post has a message before displaying it – Mihai Matei Sep 27 '16 at 08:03