Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.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_Arrays_Json - Fatal编程技术网

Php 我的json编码有什么问题?

Php 我的json编码有什么问题?,php,arrays,json,Php,Arrays,Json,我正试图用PHP做出JSON数据响应 那么我的数据代码怎么了 我的代码 $data = array( 'data' => null, 'status', 'code' => 1005 ); 输出 "data":null,"0":"status","code":1005} 这是我在输出中需要的 { "data": null, "status": { "code":

我正试图用PHP做出JSON数据响应 那么我的数据代码怎么了

我的代码

$data = array(
            'data' => null,
            'status',
            'code' => 1005
        );
输出

"data":null,"0":"status","code":1005}
这是我在输出中需要的

{
    "data": null,
    "status": {
        "code": "1005",
        "message": "Insufficient Balance",
        "datetime": "2018-03-02T01:46:19-04:00"
    }
}

尝试按如下方式设置阵列:

$data = array(
    'data' => null,
    'status' => [
        'code' => 1005,
        'message' => 'Insufficient Balance',
        'datetime' => '2018-03-02T01:46:19-04:00'

    ]
);
您可以使用以下命令将其转换为JSON:

$json = json_encode($data);

按照您编写的方式,
status
是一个值。您需要将其用作密钥,以获得所需的结果

<?php
$data = array(
            'data' => null,
            'status' => ['code' => 1005]
        );

此代码看起来不完整且格式不正确。大家好,很抱歉我编辑了此代码。请注意
状态
行与输入中的其他行有何不同?也许这就是为什么输出不是你所期望的。