php json_encode returns empty object
So I was wandering around php.net for information about serializing PHP objects to JSON, when I stumbled across the new JsonSerializable Interface.If I attempt to serialize the object directly, it returns an empty JSON string which is because I assume jsonencode() doesnt know what the I use echo jsonencode(books) But I get an empty string. Any ideas on how to convert this array to json? Extra info after first comment by Syscall and trincotreturn books 1 answer. answered 2018-02-03 12:16 Syscall. That is because vardump() displays the objects values, including private Related Discussions. 35899 [NEW]: Empty objects returned from simplexmlloadstring evaluate to false. [ PHP-BUG] Bug 54484 [NEW]: Empty string in jsondecode doesnt set/reset41505 [NEW]: jsonencode() improperly encodes PHP arrays with an empty string array key. JSONFORCEOBJECT Using this, jsonencode will return an object for given input data except for associative array.isset vs empty vs isnull. MD5 Vs SHA1 Hashing in PHP. To create an empty JSON object I do usually useWell, jsonencode() simply returns a string from a PHP array/object/etc. You can achieve the same effect much more efficiently by doing jsonencode(status, JSONFORCEOBJECT)Especially useful when the recipient of the output is expecting an object and the array is empty. Available since PHP 5.3.0. return getobjectvars(this) BookRepository.