0

I have this output when I do var_dump($myvar) on a variable.

    object(stdClass)#5 (19) {
      ["contributors"]=>
      NULL
      ["coordinates"]=>
      NULL
      ...
      ...
      ...
      ["text"]=>
      string(118) "Tune in to @Current TV this Saturday for post-debate commentary from me + @JenGranholm + Cenk Uygur #PoliticallyDirect"
    }

How do I reach the "text" attribute? I thought it would be $myvar["text"] but that gives me this error message:

Fatal error: Cannot use object of type stdClass as array

Salman A
  • 262,204
  • 82
  • 430
  • 521
Enrique Moreno Tent
  • 24,127
  • 34
  • 104
  • 189

2 Answers2

5

You have an object of stdClass, use the dereference/object-access operator:

echo $myvar->text;
knittl
  • 246,190
  • 53
  • 318
  • 364
2

If the member names are simple, you can use the -> operator:

echo $myvar->text;

You can use an alternate syntax to access members names that contain special characters (JSON decoded data often produces such cases):

echo $myvar->{'some-other-text-with-hyphens'};
Salman A
  • 262,204
  • 82
  • 430
  • 521