By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Make json_decode Return Array Instead of Stdclass Object

A problem that many will run into, is that json_decode by default returns a Stdclass Object.

Edited: 2013-03-21 07:12

To convert json data to a PHP array, all that is needed is to use the second parameter of the json_decode function. Likely one of the first problems that many will run into, is that when they use json_decode without the second parameter – maybe not knowing its there. Without using it. json_decode will just return stdClass objects. To avoid this problem entirely, the second parameter can be set to true, which will return a PHP array, as many will expect it to.

Using json decode to return PHP array

The below example shows how to return a normal PHP array, rather than the default stdclass object.


$New_array = json_decode($json_data, true);


If for some reason this does not work, your json encoded data is likely invalid.