php Elephant Icon Logo
me@grafxflow

Written by me@grafxflow

24 Nov, 2013

0

22,145

PHP convert stdClass() object into JSON data

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;

Add comment

Smart Search

119 Following
50 Followers

me@grafxflow

Hull, United Kingdom

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!

Follow