As the Php Manual say,
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()
. I found 2 ways to deal with this.
Cast the object to array.
This can be achieved as follows.
$a = (array)$object;
By accessing the key of the Object
As mentioned earlier, when you use json_decode();
function, it returns an Object of stdClass. you can access the elements of the object with the help of ->
Operator.
$value = $object->key;
One, can also use multiple keys to extract the sub elements incase if the object has nested arrays.
$value = $object->key1->key2->key3...;
Their are other options to print_r()
as well, like var_dump();
and var_export();
P.S : Also, If you set the second parameter of the json_decode();
to true
, it will automatically convert the object to an array();
Here are some references:
http://php.net/manual/en/function.print-r.php
http://php.net/manual/en/function.var-dump.php
http://php.net/manual/en/function.var-export.php