views:

519

answers:

2

Hi,

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?

+6  A: 

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

VolkerK
ah, very good...
Derek Adair
+2  A: 
$arrayDecoded = json_decode($arrayEncoded, true);

gives you an array.

Kai Chan