繁体   English   中英

JSON对象中的嵌套数组 的PHP

[英]Nested Array in JSON Object | PHP

我想实现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 
      }
   }
]

注意图像数组,它必须看起来像这样:

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

我想你需要这个...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM