Home > Article > Backend Development > How to convert object to json in php
PHP method to convert objects to json: You can use the json_encode function, such as [json_encode($object, JSON_FORCE_OBJECT);]. This function is used to JSON encode variables and returns JSON data if the execution is successful.
Function introduction:
json_encode() is used to JSON encode variables. This function returns JSON data if executed successfully, otherwise it returns FALSE .
(Learning video recommendation: java video tutorial)
Method of converting object to json string:
$json_string = json_encode($object, JSON_FORCE_OBJECT);
Difference:
json_encode($object); //结果:"[{"aa":"bb","cc":"dd"}]" json_encode($object,JSON_FORCE_OBJECT); //结果:"{"aa":"bb","cc":"dd"}"
Related recommendations: php training
The above is the detailed content of How to convert object to json in php. For more information, please follow other related articles on the PHP Chinese website!