如何创建一个带有响应的php json,如下所示,其中包含一个对象,一个包含3个项目的数组?
{"files": [
{
"name": "picture1.jpg",
"size": 902604,
"deleteType": "DELETE"
}
]}
现在我提出如下:
$info= (array ('file'=> array (array(
'name' => $name,
'size' => $data['file_size'],
'deleteType' => 'DELETE' ) ) ) );
echo json_encode(array($ info));
,响应如下
[
{
"file": [
{
"name": "1391845241happycny.jpg",
"size": 233.41,
"deleteType": "DELETE"
}
]
}
]
这不是我想要的,我不想在'文件'之前的[]
我想要的结果应该是
{
"file": [
{
"name": "1391845241happycny.jpg",
"size": 233.41,
"deleteType": "DELETE"
}
]
}
谁可以帮忙?
答案 0 :(得分:1)
$array = array('files' => array(array('name' => 'picture1.jpg', 'size' => '902604', 'deleteType' => 'DELETE')));
echo json_encode($array);
输出:
{"files":[{"name":"picture1.jpg","size":"902604","deleteType":"DELETE"}]}
答案 1 :(得分:0)
$array =
array("files"=>
array(
array(
"name"=>"picture1.jpg",
"size"=>"902604",
"deleteType"=>"DELETE"
)
)
);
echo json_encode($array);
答案 2 :(得分:0)
//{"files": [ { "name": "picture1.jpg", "size": 902604, "deleteType": "DELETE" } ]}
$array = array('files'=>( array(array("name"=>"picture1.jpg", "size"=>902604, "deleteType"=>"DELETE" ) )));
echo json_encode($array);
答案 3 :(得分:0)
您可以使用json_encode($array);
答案 4 :(得分:0)
$阵列= (阵列 ("文件" = GT; 排列 (阵列( "名称" = GT;"图片1,JPG&#34 ;, "大小" = GT; 902604, " deleteType" = GT;" DELETE" ) ) ) ); json_encode($阵列);
答案 5 :(得分:0)
<?php //{"files": [ { "name": "picture1.jpg", "size": 902604, "deleteType": "DELETE" } ]}
$name = "picture1.jpg";
$size = "902604";
$deleteType = "DELETE";
header('Content-type: application/json');
?>
{
"files": [{
"name" : "<?php echo $name; ?>",
"size" : "<?php echo $size; ?>",
"deletetype": "<?php echo $deleteType; ?>"
} ]
}
答案 6 :(得分:-1)
为此你需要
echo json_encode(array($your_array));