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()
                );
Answer from Taha Paksu on Stack Overflow
🌐
PHP
php.net › manual › en › function.json-encode.php
PHP: json_encode - Manual
If you want to convert only empty arrays to objects, simply convert them to empty object before use json_encode function.
🌐
Medium
medium.com › @onwuka_gideon › how-i-fixed-php-json-encode-returning-empty-result-e4ef97638ea1
How I fixed PHP json_encode() returning empty result | by Onwuka Gideon | Medium
May 31, 2019 - If you are building a RESTful API in PHP, then you are most likely using the json_encode function. json_encode is a PHP function that converts an array to JSON format. In this article, I will discuss how you can fix a `json_encode` function that returns an empty result or throws an error.
🌐
Hamatti
notes.hamatti.org › technology › recipes › force-empty-array-into-object-in-json_encode-in-php
Force empty array into object in json_encode in PHP : Garden of Learning by Juhis
December 31, 2024 - To force an empty array into an object, you can cast it with (object)$array. $book = [] if(empty($book)) { $book = (object)$book; } echo json_encode($book); // {}
🌐
Blogger
anvnguyen.blogspot.com › 2015 › 07 › php-jsonencode-return-empty-array.html
PHP json_encode return empty array [] instead of empty object {}
July 27, 2015 - If so, follow this simple solution: Developer just uses normal array() function and then use flag JSON_FORCE_OBJECT when encode the array: ... 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 ...
🌐
Shounak Gupte
shounakgupte.com › json_encode-returns-an-empty-string
How to fix PHP json_encode which returns an empty string
April 13, 2015 - If you are trying to grab some information through a web service in JSON format and if PHP function json_encode returns you an empty string, you need to convert the JSON object to UTF8.
🌐
W3Resource
w3resource.com › JSON › php-json-encode-function.php
PHP json_encode() function | JSON tutorial | w3resource
November 7, 2025 - <?php $w3r_one = array('php',"'MySQL'",'"SQL"','<?PHP ?>'); echo "Normal: ", json_encode($w3r_one), "\n"; echo "Tags: ", json_encode($w3r_one, JSON_HEX_TAG), "\n"; echo "Apos: ", json_encode($w3r_one, JSON_HEX_APOS), "\n"; echo "Quot: ", json_encode($w3r_one, JSON_HEX_QUOT), "\n"; echo "Amp: ", json_encode($w3r_one, JSON_HEX_AMP), "\n"; echo "All: ", json_encode($w3r_one, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP), "\n\n"; $w3r_two = array(); echo "Empty array output as array: ", json_encode($w3r_two), "\n"; echo "Empty array output as object: ", json_encode($w3r_two, JSON_FO
🌐
Elastic
elastic.co › docs › reference › elasticsearch › clients › php › configuration › dealing with json arrays and objects in php
Dealing with JSON arrays and objects in PHP | PHP
By using an explicit stdClass object, we can force the json_encode parser to correctly output an empty object, instead of an empty array. This verbose solution is the only way to acomplish the goal in PHP…
Find elsewhere
🌐
w3tutorials
w3tutorials.net › blog › why-would-json-encode-return-an-empty-string
Why Does json_encode Return an Empty String in PHP? Troubleshooting Nested Arrays — w3tutorials.net
JSON (JavaScript Object Notation) is a ubiquitous data format for exchanging information between systems, and PHP’s json_encode() function is the go-to tool for converting PHP arrays and objects into JSON strings. However, developers often encounter a frustrating issue: json_encode() returns an empty string instead of the expected JSON output, especially when working with nested arrays.
🌐
GitHub
github.com › parse-community › parse-php-sdk › issues › 319
json_encode shows empty objects · Issue #319 · parse-community/parse-php-sdk
May 30, 2017 - $lotQuery = new ParseQuery("Loteamento"); $lotQuery->notEqualTo("deletado", True); $lotQuery->descending("estagio"); $lotQuery->select(["cidade", "bairro", "estado", "logo", "slug", "nome", "tipo", "estagio"]); $lotResults = $lotQuery->find(); // this is something like json_encode($lotResults), but with Headers set $newResponse = $response->withJson($lotResults, 201); return $newResponse;
Author: parse-community
🌐
Codementor
codementor.io › community › how to debug/fix php json_encode() that returns an empty result
How to Debug/Fix PHP json_encode() that returns an empty result | Codementor
January 17, 2020 - If you are building a RESTful API in PHP, then you are most likely using the PHP's json_encode function. json_encode is a PHP function that converts an array to JSON. In this article, I will discuss how you can fix a json_encode function that returns an empty result or throws an error.
🌐
GitHub
gist.github.com › Alexander-Pop › 368aa6389b707e6bed20e0d8b99441c4
Debug PHP json_encode() that returns an empty result #json #debug #php · GitHub
Debug PHP json_encode() that returns an empty result #json #debug #php · Raw · DebugPHP_json_encode.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Juha-Matti Santala
hamatti.org › snacks › force-empty-php-array-to-become-json-object
Force empty PHP array to become JSON object : Juha-Matti Santala
There are no empty associative arrays in PHP so to get one converting correctly in JSON, you need extra casting.
Top answer
1 of 6
4

You can validate at following website: http://jsonlint.com/

You have to use a php "json_decode()" function to decode a json encoded data. Basically json_decode() function converts JSON data to a PHP array.

Syntax: json_decode( data, dataTypeBoolean, depth, options )

data : - The json data that you want to decode in PHP.

dataTypeBoolean(Optional) :- boolean that makes the function return a PHP Associative Array if set to "true", or return a PHP stdClass object if you omit this parameter or set it to "false". Both data types can be accessed like an array and use array based PHP loops for parsing.

depth :- Optional recursion limit. Use an integer as the value for this parameter.

options :- Optional JSON_BIGINT_AS_STRING parameter.

Now Comes to your Code

$json_string = '{"a":1,"b":2,"c":3,"d":4,"e":5}' ;

Assign a valid json data to a variable $json_string within single quot's ('') as json string already have double quots.

// here i am decoding a json string by using a php 'json_decode' function, as mentioned above & passing a true parameter to get a PHP associative array otherwise it will bydefault return a PHP std class objecy array.

$json_decoded_data = json_decode($json_string, true);

// just can check here your encoded array data.
// echo '<pre>';
// print_r($json_decoded_data);

// loop to extract data from an array
foreach ($json_decoded_data as $key => $value) {
    echo "$key | $value <br/>";

}
2 of 6
3

you should not use echo because it is an array. use print_r or var_dump .it works fine

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
print_r(json_decode($json, true));

Output:

Array
(
   [a] => 1
   [b] => 2
   [c] => 3
   [d] => 4
   [e] => 5
)