php Elephant Icon Logo
me@grafxflow

Written by me@grafxflow

24 Nov, 2013
0
28,302

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;

Share this article

Add comment...

Smart Search

me@grafxflow Author Profile picture

me@grafxflow

Founder, 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. I build lighting-fast dynamic content engines with Laravel, Winter CMS and Tailwind CSS. Thanks for stopping by!