Written by me@grafxflow
24 Nov, 2013
0
22,145
I was recently working on a website were the PHP function was doing a var_dump of the array and making it into a stdClass. The problem that I had was that there was also some jQuery code that needed to validate the data had been retrieved successfully. So the stdClass object needed to be converted to JSON data.
Here is how...
$example_object = new stdClass();
$example_object->name = "Joe Bloggs";
$example_object->jobtitle = "Developer";
$json_data = json_encode((array) $example_object);
print_r($json_data);
exit;
If there is a specific group or value that needs to be output as JSON data then use.
$example_object = new stdClass();
$example_object->name = "Joe Bloggs";
$example_object->jobtitle = "Developer";
$json_data = json_encode((array) $example_object->name);
print_r($json_data);
exit;
07 Nov, 2012
26 Apr, 2018
30 Apr, 2017
I am a Full-stack Developer who also started delving into the world of UX/UI Design a few years back. I blog and tweet to hopefully share a little bit of knowledge that can help others around the web. Thanks for stopping by!
Follow11 Jul, 2023
21 Jun, 2023
25 Nov, 2022
20 Sep, 2022
Views: 162,134
Views: 38,598
Views: 34,154
Views: 31,639