0

The PHP function json_decode (by default) returns an object. Switching the second argument will return an array.

Maybe I just don't understand objects, but I thought objects have properties and methods (maybe events too). Arrays only have properties.

Given that json_decode will only ever return properties and never methods, shouldn't it always return an array?

3
  • 1
    As objects can have methods or events, it is completely valid to return the decoded result as an object. Commented Sep 13, 2012 at 6:34
  • To clarify what alex said... JSON = Javascript OBJECT Notation. Commented Sep 13, 2012 at 6:34
  • 2
    ... I'm not using JavaScript (it doesn't even output to the client). I'm not using Objects. Should I call it PAN (PHP Array Notation)? Commented Sep 13, 2012 at 6:38

4 Answers 4

10

It returns an object because JSON defines an object structure. This is what the 'O' stands for in 'JSON'.

This is where the differences between languages starts to become more obvious.

Javascript uses objects where PHP might use an array with named keys. JS can't have named keys in an array, only in an object. Other languages have other limitations to how they structure their variables.

Using an object means that PHP is as consistent with other language implementations of JSON as possible. Since JSON is designed for cross-language communication, being consistent is important.

But as you say, in PHP it is sometimes easier to work with an array, so this is why PHP offers the option of converting it directly to an array.

But be aware that PHP arrays are not the same as JSON arrays and objects. PHP allows you to mix named and numbered array keys. This does not map well to JSON, so if you're using PHP arrays to work with JSON you have to be careful of it. If you're using PHP objects for your JSON work, then you won't have this mismatch in functionality.

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

Comments

3

Okay, so it seems you already knew this:

By default, json_decode will return a stdClass object. If you want an array, use:

json_decode($jsondata, true);

See: http://php.net/manual/en/function.json-decode.php

So, to answer "Why":

JSON is a format used to store hierachical datasets, much like how XML might have been used in the past. Because however Javascript is optimized for accessing object properties, no additional libraries need be present to work with JSON structures - they are actual objects in Javascript.

It is easier to parse JSON than XML, and relatively easy to translate into objects and/or arrays in back-end languages. In many languages outside of PHP, there is something called a Dictionary, or Hashtable, which is usually an object with key/value pairs.

PHP does not differentiate arrays and "associative arrays" other than contextually, so for a PHP developer it's natural to expect the result to be an associative array, and that option exists, but most likely for flexibility (and maybe because it decodes more naturally to the object) the object format exists.

I hope that explains. I also strongly recommend reading further on what JSON is (and is not) here: http://json.org

1 Comment

he knows that. he mentioned it in the first paragraph. he's not asking 'how', he's asking 'why'.
0

json_decode returns by default an object from the stdClass class. This is the basic (top-level) generic class for objects. This class has no method nor attributes first.

But then you can add some "on the fly", what's called Dynamic Properties. More here:

Sometimes all that is necessary is a property bag to throw key value pairs into. One way is to use array, but this requires quoting all keys. Another way is to use dynamic properties on an instance of StdClass.

Hope it helps.

Comments

0

It returns the object of stdClass. If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. Arrays convert to an object with properties named by keys, and corresponding values. For any other value, a member variable named scalar will contain the value.

Comments

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.