Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 61 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 PHP 4 json encode / decode library (that is even PHP 5 reverse compatible) written about in this blog post: Using json_encode() and json_decode() in PHP4 (Jun 2009).

The concrete code is by Michal Migurski and by Matt Knapp:

share|improve this answer

Your Answer

 
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.