Written by me@grafxflow
24 Nov, 2013
0
24,362
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
22 Nov, 2009
26 Apr, 2018
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
Views: 166,598
Views: 40,467
Views: 37,239
Views: 33,739