10

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 ?

1
  • Welcome to Stackoverflow. A few seconds spent reading the manual entry would have saved you the trouble of asking. Commented Mar 11, 2014 at 19:13

1 Answer 1

14

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

json_decode($json, true)

Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
You would like to use it together, it would work like urlencode((json_decode($json, true)).
But, what is a multi-dimensional array of objects is passed? It is converting objects into arrays recursively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.