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:

Php Code
Fatal error: Cannot use object of type stdClass as array in
C:\Users\Wiki\software\wikitechy.php on line 108

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 :

Php Code
var_dump($result->context);

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:

Php Code
var_dump($result->{'from-date'});
  • 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:
Php Code
var_dump($result->{'from-date'});
  • If we need an array we can do the following:
Php Code
$result = json_decode($json, true);
  • Or cast the object to an array:
Php Code
$result = (array) json_decode($json);

  • Use the second parameter of json_decode to make it return an array:
Php Code
$result = json_decode($data, true);
[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:
Php Code
$my_array = json_decode($my_json, true);

  • 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 :

Php Code
$array = (array)$stdClass;
[ad type=”banner”]

function:

Php Code
mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
  • 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:

Php Code
$result['context'];

To:

Php Code
$result->context;

  • 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.

php Code
$a = (array)$object;

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.

Php Code
$value = $object->key;
  • Use multiple keys to extract the sub elements incase if the object has nested arrays.
Php Code
$value = $object->key1->key2->key3...;

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

[ad type=”banner”]

Categorized in: