How to make PHP stdClass Object with the same name?

I am working against an API that gives me a stdClass object that looks like this (actual data replaced)

"data": [
    {
      "type": "image",
      "comments": {
        "data": [
          {
            "created_time": "1346054211",
            "text": "Omg haha that a lot of squats",
            "from": {},
            "id": "267044541287419185"
          },
          {
            "created_time": "1346054328",
            "text": "Fit body",
            "from": {},
            "id": "267045517536841021"
          },
        ]
      },
      "created_time": "1346049912",
    },

How can I create a stdClass object, for example, "Comments", which have several subfields with the same name and different data. When I try to create a stdClass that looks like this, my comment section will only contain 1 input, which is the last in the while loop. Therefore, instead of applying to the bottom, it replaces the old data with the new. How to fix it?

+5
source share
1 answer

"comments" - "data", . , JSON, PHP, stdClass . .

$comments = new stdClass;
$comments->data = array();

for ($i = 0; $i < 2; $i++) {
    $comment = new stdClass;
    $comment->text = 'Lorem ipsum...';
    ...
    $comments->data[] = $comment;
}

var_dump($comments);
+11

All Articles