We get a strange error using json_decode(). It decode correctly the data, but when we try to access info inside the array we get the following:

[pastacode lang=”php” manual=”Fatal%20error%3A%20Cannot%20use%20object%20of%20type%20stdClass%20as%20array%20in%0AC%3A%5CUsers%5CWiki%5Csoftware%5Cwikitechy.php%20on%20line%20108%0A” message=”Php Code” highlight=”” provider=”manual”/]

We tried to do: $result[‘context’] where $result has the data returned by json_decode()

php json

The function json_decode() returns an object by default.

Access the data :

[pastacode lang=”php” manual=”var_dump(%24result-%3Econtext)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

If we have identifiers like from-date (the hyphen would cause a PHP error when using the above method) we have to write the following:

[pastacode lang=”php” manual=”var_dump(%24result-%3E%7B’from-date’%7D)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • If we have identifiers like from-date (the hyphen would cause a PHP error when using the above method) we have to write the following:
[pastacode lang=”php” manual=”var_dump(%24result-%3E%7B’from-date’%7D)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • If we need an array we can do the following:
[pastacode lang=”php” manual=”%24result%20%3D%20json_decode(%24json%2C%20true)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • Or cast the object to an array:
[pastacode lang=”php” manual=”%24result%20%3D%20(array)%20json_decode(%24json)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

  • Use the second parameter of json_decode to make it return an array:
[pastacode lang=”php” manual=”%24result%20%3D%20json_decode(%24data%2C%20true)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

  • Use true as the second parameter to json_decode.
  • This will decode the json into an associative array instead of stdObject instances:
[pastacode lang=”php” manual=”%24my_array%20%3D%20json_decode(%24my_json%2C%20true)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

  • If we call json_decode($somestring) we will get an Object and we need to access like $object->key,
  • But if we call json_decode($somestring, true) we will get an array and can access like $array[‘key’]

We can convert stdClass object to array :

[pastacode lang=”php” manual=”%24array%20%3D%20(array)%24stdClass%3B%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

function:

[pastacode lang=”php” manual=”mixed%20json_decode%20(%20string%20%24json%20%5B%2C%20bool%20%24assoc%20%3D%20false%20%5B%2C%20int%20%24depth%20%3D%20512%20%5B%2C%20int%20%24options%20%3D%200%20%5D%5D%5D%20)%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • When param is false, which is default, it will return an appropriate php type.
  • When param is true, it will return associative arrays.
  • It will return NULL on error.
  • If we want to fetch value through array, set assoc to true.

we must access it using -> since its an object.

Change our code from:

[pastacode lang=”php” manual=”%24result%5B’context’%5D%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

To:

[pastacode lang=”php” manual=”%24result-%3Econtext%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

  • print_r — Prints human-readable information about a variable
  • When we use json_decode();, we get an object of type stdClass as return type.
  • The arguments, which are to be passed inside of print_r() should either be an array or a string. Hence, we cannot pass an object inside of print_r(). There are two ways to deal with this.

1. Cast the object to array.
This can be achieved as follows.

[pastacode lang=”php” manual=”%24a%20%3D%20(array)%24object%3B%0A” message=”php Code” highlight=”” provider=”manual”/]

2. By accessing the key of the Object
when we use json_decode(); function, it returns an Object of stdClass. we can access the elements of the object with the help of -> Operator.

[pastacode lang=”php” manual=”%24value%20%3D%20%24object-%3Ekey%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • Use multiple keys to extract the sub elements incase if the object has nested arrays.
[pastacode lang=”php” manual=”%24value%20%3D%20%24object-%3Ekey1-%3Ekey2-%3Ekey3…%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

Their are many options to print_r() like var_dump(); and var_export();

[ad type=”banner”]

Categorized in: