Blog

24 Nov
2013

php Elephant Icon Logo

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;
Tagged: < convert | javascript | jquery | json | object | php | stdclass >
Posted in: < PHP and HTML and CSS >
me@grafxflow avatar

me@grafxflow


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!

Visitors also viewed these posts


Add comment

Add comment