1

Fatal error: Uncaught Error: Cannot use object of type stdClass as array

$code = json_decode($src);
echo $code["items"];

var_dump shows the following (truncated):

object(stdClass)#7 (3) { ... } 

I don't know what stdClass is and how to use it.

Edit:

stdClass is a class which creates an instance of simple object. Properties of classes are to be accessed using ->, not [...] notation.

As per documentation for json_decode, simply setting the second argument to true will result in the result being associative array, which can be in turn accessed like an array.

At the time of posting this question, I didn't try searching on how to decode JSON - as that's pretty simple and I got that working. I was just getting another error (above), and had no luck searching on how to fix that. I believe people have similar problems, as this question is getting some views as well.

Dan B.
  • 93
  • 1
  • 1
  • 12
  • If you _know_ it is a duplicate, please do StackOverflow and volunteers a favor and don't post it. Take the time that it took you to post the question and just search SO. – mickmackusa Jan 28 '18 at 12:16
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – mickmackusa Jan 29 '18 at 01:02

2 Answers2

9

Use json_decode($src, true) to get associative array.

This is preferred way, as currently you get mixed arrays and objects and you may end up in crazy house, trying to work with these :)

Alternatively use -> operator to get properties of object.

Currently your item is at:

$code->items[0]->pagemap->cse_image[0]->src
Rauli Rajande
  • 2,010
  • 1
  • 20
  • 24
-1

The json_decode function has a second parameter dedicated to just that: setting it to true will return an associative array where there is an object (in { "curly": "braces" }) in JSON.

To illustrate that:

$a = json_decode('{ "b": 1 }', false);
var_dump($a->b);
// prints: int(1)

$a = json_decode('{ "b": 1 }', true);
var_dump($a['b']);
// prints: int(1)

Note the difference in how values are accessed.

Further reading: http://php.net/json_decode

ob-ivan
  • 785
  • 1
  • 8
  • 17
  • For the record, this answer is not wrong. Proof: http://sandbox.onlinephpfunctions.com/code/8c238cf872cdc36a335be6cdb63182681948440f Normally I would neutralize this downvote, but this answer does very little to educate and Rauli's answer is more comprehensive with two ways to correct the problem. (ob-ivan you may delete this question to regain your 2 lost rep points) – mickmackusa Jan 30 '18 at 06:05
  • @mickmackusa Thank you for the advice. In fact, I'm grateful to whoever downvoted this because it reminded me SO is there for *great* answers, not quick ones. I guess I'll leave it here, though ---for the record. – ob-ivan Jan 30 '18 at 07:55
  • If you are going to leave the answer please update it to provide more/useful unique information. If it helps someone else, you may receive a neutralizing vote and more. – mickmackusa Jan 30 '18 at 08:16
  • @mickmackusa Thank you. It is difficult to provide unique information when citing official documentation. I hope at least it makes a better digest on a particular topic than the original. – ob-ivan Jan 30 '18 at 11:55
  • please add formatted "comment" underneath each `var_dump` to express the output. Like: `// output: blah,blah` – mickmackusa Jan 30 '18 at 11:57
  • @mickmackusa Done. BTW, is there a summarized guideline which would contain all these pieces of advice you share? It could save both of us some time and keystrokes. – ob-ivan Jan 30 '18 at 18:16
  • Nothing I can think of offhand. My recommendation is that you always _prove_ your answers with a demo link when possible. This ensures that you haven't made any silly syntax errors and removes any doubts about effectiveness. Never post code-only answers. Never post duplicate info; if you are late, either delete it or add unique value (not just links to the manual) so that it is not mere page bloat. Always explain your solutions with the intent to educate thousands of future SO readers. – mickmackusa Feb 03 '18 at 08:58