Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_encode() automatically converts array into object

Tags:

json

php

When I encode an array with json_encode() in PHP, all gets done right till here, but when I decode it back with json_decode(), it gives me stdClass Object instead of an Array. Here is my code:

echo $json=json_encode(array('a'=>1,'b'=>2));
echo '<br>';
print_r(json_decode($json));

// and the result of PHP script is :
{"a":1,"b":2}
// stdClass Object ( [a] => 1 [b] => 2 )

what converts it into object from an array ??

can i pass the json encoded string into url to get data on another page, or there exists any function that can do some url encoding ?

like image 650
viral Avatar asked Mar 11 '14 19:03

viral


1 Answers

json_decode() needs an extra parameter to decode to an array.

json_decode($json, true)

like image 184
Anriëtte Myburgh Avatar answered Sep 29 '22 03:09

Anriëtte Myburgh