Observe this little script:

$array = array('stuff' => 'things');
print_r($array);
//prints - Array ( [stuff] => things )
$arrayEncoded = json_encode($array);
echo $arrayEncoded . "<br />";
//prints - {"stuff":"things"}
$arrayDecoded = json_decode($arrayEncoded);
print_r($arrayDecoded);
//prints - stdClass Object ( [stuff] => things )

Why does PHP turn the JSON Object into a class?

Shouldn't an array that is json_encoded then json_decoded yield the EXACT same result?

share|improve this question

3 Answers

up vote 47 down vote accepted

Take a closer look at the second parameter of json_decode($json, $assoc, $depth) at http://docs.php.net/json_decode

share|improve this answer
ah, very good... – Derek Adair Feb 17 '10 at 15:39
Thanks! That was perfect! – TecBrat Jul 18 '12 at 14:44
$arrayDecoded = json_decode($arrayEncoded, true);

gives you an array.

share|improve this answer

There is also a good php4 json encode / decode library (that is even php5 reverse compatible) on this site:

http://techblog.willshouse.com/2009/06/12/using-json_encode-and-json_decode-in-php4/

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.