Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中为json创建对象和数组_Php_Json - Fatal编程技术网

在php中为json创建对象和数组

在php中为json创建对象和数组,php,json,Php,Json,如何创建一个响应如下的php json,其中包含一个对象、一个数组和3个项目 {"files": [ { "name": "picture1.jpg", "size": 902604, "deleteType": "DELETE" } ]} 现在我提出如下: $info= (array ('file'=> array (array( 'name' => $name, 'size' =

如何创建一个响应如下的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_编码(数组($info))

回应如下

[
    {
        "file": [
            {
                "name": "1391845241happycny.jpg",
                "size": 233.41,
                "deleteType": "DELETE"
            }
        ]
    }
]
这不是我想要的,我不想在'file'之前使用[]

我想要的结果应该是

{
    "file": [
        {
            "name": "1391845241happycny.jpg",
            "size": 233.41,
            "deleteType": "DELETE"
        }
    ]
}
谁能帮忙?

为此,您需要

echo json_encode(数组($your_数组))

输出:

{"files":[{"name":"picture1.jpg","size":"902604","deleteType":"DELETE"}]}

您可以使用
json\u encode($array)

$array= (数组 (“文件”=> 排列 (数组( “名称”=>“图片1,jpg”, “尺寸”=>902604, “deleteType”=>“删除” ) ) ) ); json_编码($array)


{
“文件”:[{
“名称”:“,
“大小”:“,
“删除类型”:”
} ]
}

将缺少的数组形象化EP:P我现在通过查看您的答案知道了..谢谢!响应变成[{“files”:[{“name”:“1391845241happycny.jpg”,“size”:233.41,“deleteType”:“DELETE”}]}],在“files”之前得到[]文件变成数组,而不是数组object@user3286508否响应为{“files”:[{“name”:“picture1.jpg”,“size”:902604,“deleteType”:“DELETE”}]}$info=(数组('file'=>array(数组))('name'=>$name,'size'=>$data['file_size'],'deleteType'=>'DELETE'));有错误吗?echo json_encode(数组($info));
$array = array('files' => array(array('name' => 'picture1.jpg', 'size' => '902604', 'deleteType' => 'DELETE')));
echo json_encode($array);
{"files":[{"name":"picture1.jpg","size":"902604","deleteType":"DELETE"}]}
//{"files": [ { "name": "picture1.jpg", "size": 902604, "deleteType": "DELETE" } ]}


$array  =   array('files'=>( array(array("name"=>"picture1.jpg", "size"=>902604, "deleteType"=>"DELETE" ) )));


echo json_encode($array);
<?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; ?>"
   } ]
}