json_encode function not return Braces {} when array is empty in php
PhpArraysJsonPhp Problem Overview
I have this code
$status = array(
"message"=>"error",
"club_id"=>$_club_id,
"status"=>"1",
"membership_info"=>array(),
);
echo json_encode($status);
This function return json:
{"message":"error","club_id":275,"status":"1","membership_info":[]}
But I need json like this:
{"message":"error","club_id":275,"status":"1","membership_info":{}}
Php Solutions
Solution 1 - Php
use the JSON_FORCE_OBJECT
option of 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.
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()
);
Solution 2 - Php
$status = array(
"message"=>"error",
"club_id"=>$_club_id,
"status"=>"1",
"membership_info"=>(object) array(),
);
By casting the array into an object, json_encode
will always use braces instead of brackets for the value (even when empty).
This is useful when can't use JSON_FORCE_OBJECT
and when you can't (or don't want) to use an actual object for the value.
Solution 3 - Php
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_OBJECT
flag, available since PHP 5.3. See http://php.net/json_encode. Note that this will apply to all arrays.
Alternatively you could actually use objects in your PHP code instead of arrays:
$data = new stdClass;
$data->foo = 'bar';
...
Maybe it's simpler to handle the edge case of empty arrays client-side.
Solution 4 - Php
I know this is an old question, but it's among the first hits on Google, so I thought I should share an alternative solution.
Rather than using standard PHP arrays, in PHP 7+ you can instead use Map()
as part of the Data Structure extension. Documentation.
A Map object has practically identical performance as arrays and also implements ArrayAccess so it can be accessed as a regular array. Contrary to a standard array, however, it will always be associative and works as expected with json_encode
. It also has some other minor benefits like object keys and better memory handling.
Some example usage:
use Ds\Map;
$status = new Map([
"message"=>"error",
"club_id"=>$_club_id,
"status"=>"1",
"membership_info"=>array(),
]);
$map = new Map();
print json_encode($map); // {}
$map = new Map();
$map["foo"] = "bar";
print json_encode($map); // {"foo":"bar"}
print $map["foo"]; // bar
$map = new Map();
$map[1] = "foo";
$map[2] = "bar";
$map[3] = "baz";
print json_encode($map); // {"1":"foo","2":"bar","3":"baz"}
Solution 5 - Php
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_OBJECT
converting a normal array into an object.