-1

I'm trying export data to variables. I have data in this form:

{
  "data":[{
    "unread": 1,
    "id":"1111",
    "updated_time":"2015-01-21T00:00:38+0000",
    "comments":{
      "data":[{
        "id":"1111_2222",
        "from":
        {
          "id":"9999",
          "name":"John"
        },
        "message":"Hello Steve, how are you?",
        "created_time":"2015-01-21T00:00:38+0000"
      }]
    }
  }]
}

I have following code but it exports only unread, id and updated_time. However I need even data from comments (message, id, name - from).

foreach ($fb_response->data as $item) {
    echo 'unread: ' . $item->unread . '<br />';
    echo 'From ID: ' . $item->id . '<br />';
    echo 'Time: ' . $item->updated_time;
}
t j
  • 7,026
  • 12
  • 46
  • 66

2 Answers2

1

Just another loop?

$comments = $item->comments->data;
foreach ($comments as $comment) {
    echo $comment->id;
    echo $comment->from->id;
    echo $comment->from->name;
    echo $comment->message;
    echo $comment->created_time;
}

Hope this helps.

Dan Belden
  • 1,199
  • 1
  • 9
  • 20
0

Your last echo was not terminated properly. Try this:

foreach($fb_response->data as $item){
echo 'unread: ' . $item->unread . '<br />';
echo 'From ID: ' . $item->id . '<br />';
echo 'Time: ' . $item->updated_time ;
}
Clyde Winux
  • 285
  • 1
  • 3
  • 12