MerceloCefai
MerceloCefai

Reputation: 3

Nested Array in JSON Object | PHP

I want to achieve the following format output of the JSON Object:

[  
   {  
      "id":1,
      "title":"Test Title",
      "url":"http://test.com/",
      "images":[  
         {  
            "width":100,
            "height":100,
            "size":17000,
            "url":"http://test.com",
            "timestamp":14566698
         },
         {  
            "width":100,
            "height":100,
            "size":160000,
            "url":"http://test.com",
            "timestamp":1451903339
         }
      ]
   }
]

I am gathering all the data from a database and saving them into variables and using PHP to create the JSON object including also a loop as it needs to create more than one property:

for ($x = 1; $x <= 2; $x++) {

    $JSONarray[] = array(
        'id' => $x,
        'title' => $title,
        'url' => $url,
        'images' => array(
            'width' => $width,
            'height' => $height,
            'size' => $size,
            'url' => urldecode($image),
            'timestamp' => $timestamp
        ),
        array(
            'width' => $width2,
            'height' => $height2,
            'size' => $size2,
            'url' => urldecode($image2),
            'timestamp' => $timestamp2
        )
    );
}

echo json_encode($JSONarray, JSON_UNESCAPED_SLASHES);

However the output I am achieving is not the one I intent to achieve. The output that I am getting is the below:

[  
   {  
      "id":1,
      "title":"Test Title",
      "url":"http://test.com/",
      "images":{  
         "width":100,
         "height":10,
         "size":17000 ,
         "url":"http://test.com/",
         "timestamp":14566698 
      },
      "0":{  
         "width":100,
         "height":100,
         "size":160000 ,
         "url":"http://test.com/",
         "timestamp":1451903339 
      }
   }
]

Upvotes: 0

Views: 892

Answers (2)

Alaa M. Jaddou
Alaa M. Jaddou

Reputation: 1189

I think you need this...

for ($x = 1; $x <= 2; $x++) {

    $JSONarray[] = array(
        'id' => $x,
        'title' => $title,
        'url' => $url,
        'images' => array(
            array(
                 'width' => $width,
                 'height' => $height,
                 'size' => $size,
                 'url' => urldecode($image),
                 'timestamp' => $timestamp
            ),
            array(
                 'width' => $width2,
                 'height' => $height2,
                 'size' => $size2,
                 'url' => urldecode($image2),
                 'timestamp' => $timestamp2
            )
        )
    );
}
echo json_encode($JSONarray, JSON_UNESCAPED_SLASHES);

Upvotes: 0

Nick
Nick

Reputation: 10143

Pay attention to images array, it must looks like this:

for ($x = 1; $x <= 2; $x++) {

    $JSONarray[] = array(
        'id' => $x,
        'title' => $title,
        'url' => $url,
        'images' => array(
            (object)array(
                'width' => $width,
                'height' => $height,
                'size' => $size,
                'url' => urldecode($image),
                'timestamp' => $timestamp
            ),
            (object)array(
                'width' => $width2,
                'height' => $height2,
                'size' => $size2,
                'url' => urldecode($image2),
                'timestamp' => $timestamp2
            )
        )
    );
}

Upvotes: 2

Related Questions