JSON对象中的嵌套数组| PHP

时间:2016-01-06 16:21:33

标签: php arrays json

我想实现JSON对象的以下格式输出:

[  
   {  
      "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
         }
      ]
   }
]

我正在收集数据库中的所有数据并将它们保存到变量中,并使用PHP创建JSON对象,还包括一个循环,因为它需要创建多个属性:

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);

然而,我实现的输出并不是我想要实现的输出。我得到的输出如下:

[  
   {  
      "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 
      }
   }
]

2 个答案:

答案 0 :(得分:2)

注意图像阵列,它必须如下所示:

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
            )
        )
    );
}

答案 1 :(得分:0)

我认为你需要这个......

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);