127

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?

Teun Zengerink
4,4235 gold badges34 silver badges32 bronze badges
asked Feb 17, 2010 at 15:35

6 Answers 6

101
$arrayDecoded = json_decode($arrayEncoded, true);

gives you an array.

answered Feb 17, 2010 at 15:38
Sign up to request clarification or add additional context in comments.

2 Comments

This is so woefully helpful, thank you @Kai Chain - I suppose the original question was asking, "Why", but this seems to get at what they intended. Whatever the case, perfect for my situation.
And does the same job after 4 years here! Basic knowledge that everyone should actually study first, like me!
31

To answer the actual question:

Why does PHP turn the JSON Object into a class?

Take a closer look at the output of the encoded JSON, I've extended the example the OP is giving a little bit:

$array = array(
 'stuff' => 'things',
 'things' => array(
 'controller', 'playing card', 'newspaper', 'sand paper', 'monitor', 'tree'
 )
);
$arrayEncoded = json_encode($array);
echo $arrayEncoded;
//prints - {"stuff":"things","things":["controller","playing card","newspaper","sand paper","monitor","tree"]}

The JSON format was derived from the same standard as JavaScript (ECMAScript Programming Language Standard) and if you would look at the format it looks like JavaScript. It is a JSON object ({} = object) having a property "stuff" with value "things" and has a property "things" with it's value being an array of strings ([] = array).

JSON (as JavaScript) doesn't know associative arrays only indexed arrays. So when JSON encoding a PHP associative array, this will result in a JSON string containing this array as an "object".

Now we're decoding the JSON again using json_decode($arrayEncoded). The decode function doesn't know where this JSON string originated from (a PHP array) so it is decoding into an unknown object, which is stdClass in PHP. As you will see, the "things" array of strings WILL decode into an indexed PHP array.

Also see:


Thanks to https://www.randomlists.com/things for the 'things'

answered Sep 8, 2015 at 15:38

1 Comment

this is the correct answer... it answers the question, whereas the other ones give workarounds to an unexplained problem. Thanks, I was going in circles forgetting that JSON didn't support associative arrays!
7

Although, as mentioned, you could add a second parameter here to indicate you want an array returned:

$array = json_decode($json, true);

Many people might prefer to cast the results instead:

$array = (array)json_decode($json);

It might be more clear to read.

answered Nov 30, 2016 at 19:40

1 Comment

There are different. Be aware if you have encoded a multidimensional array or object. First one gives you array of arrays, second one gives you array of objects.
2

tl;dr: JavaScript doesn't support associative arrays, therefore neither does JSON.

After all, it's JSON, not JSAAN. :)

So PHP has to convert your array into an object in order to encode into JSON.

answered Jan 31, 2016 at 10:32

Comments

1
 var_dump(json_decode('{"0":0}')); // output: object(0=>0)
 var_dump(json_decode('[0]')); //output: [0]
 var_dump(json_decode('{"0":0}', true));//output: [0]
 var_dump(json_decode('[0]', true)); //output: [0]

If you decode the json into array, information will be lost in this situation.

answered Jul 16, 2018 at 7:44

Comments

0

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:

hakre
200k55 gold badges454 silver badges868 bronze badges
answered Jan 26, 2011 at 2:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.