当 php 中的数组为空时,json_encode 函数不返回大括号 {}

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28189390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:43:47  来源:igfitidea点击:

json_encode function not return Braces {} when array is empty in php

phparraysjson

提问by Umashankar Saw

I have this code

我有这个代码

$status = array(
                "message"=>"error",
                "club_id"=>$_club_id,
                "status"=>"1",
                "membership_info"=>array(),
                );

echo json_encode($status);

echo json_encode($status);

This function return json:
{"message":"error","club_id":275,"status":"1","membership_info":[]}

这个函数返回json:
{"message":"error","club_id":275,"status":"1","membership_info":[]}

But I need json like this:

但我需要这样的 json:

{"message":"error","club_id":275,"status":"1","membership_info":{}}

{"message":"error","club_id":275,"status":"1","membership_info":{}}

回答by Taha Paksu

use the JSON_FORCE_OBJECToption of json_encode:

使用以下JSON_FORCE_OBJECT选项json_encode

json_encode($status, JSON_FORCE_OBJECT);

Documentation

文档

JSON_FORCE_OBJECT (integer)Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty. Available since PHP 5.3.0.

JSON_FORCE_OBJECT (integer)使用非关联数组时输出对象而不是数组。当输出的接收者期待一个对象并且数组为空时特别有用。自 PHP 5.3.0 起可用。

Or, if you want to preserve your "other" arrays inside your object, don't use the previous answer, just use this:

或者,如果你想在你的对象中保留你的“其他”数组,不要使用以前的答案,只需使用这个:

$status = array(
                "message"=>"error",
                "club_id"=>$_club_id,
                "status"=>"1",
                "membership_info"=> new stdClass()
                );

回答by humbleice

$status = array(
                "message"=>"error",
                "club_id"=>$_club_id,
                "status"=>"1",
                "membership_info"=>(object) array(),
                );

By casting the array into an object, json_encodewill always use braces instead of brackets for the value (even when empty).

通过将数组转换为对象,json_encode将始终使用大括号而不是括号作为值(即使为空)。

This is useful when can't use JSON_FORCE_OBJECTand when you can't (or don't want) to use an actual object for the value.

这在不能使用JSON_FORCE_OBJECT以及不能(或不想)使用实际对象作为值时很有用。

回答by deceze

There's no difference in PHP between an array and an "object" (in the JSON sense of the word). If you want to force all arrays to be encoded as JSON objects, set the JSON_FORCE_OBJECTflag, available since PHP 5.3. See http://php.net/json_encode. Note that this will apply to allarrays.

PHP 中的数组和“对象”(在这个词的 JSON 意义上)之间没有区别。如果要强制将所有数组编码为 JSON 对象,请设置该JSON_FORCE_OBJECT标志,自 PHP 5.3 起可用。请参阅http://php.net/json_encode。请注意,这将适用于所有数组。

Alternatively you could actually use objectsin your PHP code instead of arrays:

或者,您实际上可以在 PHP 代码中使用对象而不是数组:

$data = new stdClass;
$data->foo = 'bar';
...

Maybe it's simpler to handle the edge case of empty arrays client-side.

也许在客户端处理空数组的边缘情况更简单。

回答by anushr

While this may not be considered elegant, a simple string replace can effectively address this.

虽然这可能不被认为是优雅的,但简单的字符串替换可以有效地解决这个问题。

str_replace("[]", "{}", json_encode($data));

This mitigates the issue of JSON_FORCE_OBJECTconverting a normal array into an object.

这减轻了JSON_FORCE_OBJECT将普通数组转换为对象的问题。