Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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 Yi2-如何强制响应JSON格式化程序使用对象而不是数组?_Php_Json_Yii_Yii2_Httpresponse - Fatal编程技术网

Php Yi2-如何强制响应JSON格式化程序使用对象而不是数组?

Php Yi2-如何强制响应JSON格式化程序使用对象而不是数组?,php,json,yii,yii2,httpresponse,Php,Json,Yii,Yii2,Httpresponse,我知道我可以设置Yii::$app->response->format=\Yii\web\response::format\u JSON,但是我如何设置JSON编码标志JSON\u FORCE\u OBJECT,这样所有数组都将被编码为对象呢?好吧,这并不难: Yii::$app->response->formatters[\Yii\web\response::FORMAT\u JSON]=[ 'class'=>'yii\web\jsonresponseformter', 'encodeOpti

我知道我可以设置
Yii::$app->response->format=\Yii\web\response::format\u JSON
,但是我如何设置JSON编码标志
JSON\u FORCE\u OBJECT
,这样所有数组都将被编码为对象呢?

好吧,这并不难:

Yii::$app->response->formatters[\Yii\web\response::FORMAT\u JSON]=[
'class'=>'yii\web\jsonresponseformter',
'encodeOptions'=>JSON\u FORCE\u对象,
];
甚至作为一个班轮:

Yii::$app->response->formatters[\yii\web\Response::FORMAT_JSON]['encodeOptions'] = JSON_FORCE_OBJECT;