1

This should be a simple issue, but I can't seem to get the past the syntax.

    object(stdClass)#15 (12) {
  ["im:name"]=>
  object(stdClass)#14 (1) {
    ["label"]=>
    string(15) "Balls VS Blocks"
  }

A simple $info->im:name->label should be able to extract "Balls VS Blocks". However since there is an : in the first key, it's throwing off my IDE and then it of course throws an error. How can I escape this special character in the key?

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
Chad
  • 643
  • 2
  • 11
  • 22
  • I disagree that this a duplicate question, as I searched Stack Overflow for a while before asking the question. The issue with duplicate is that it does not address php or json in the title. Hence doing searches it did not come up in search. In the event had I found the duplicate, I would have not asked the question as I was pretty certian this question had been asked previously. So this is an issue of SEO rather than anything else... Oh well... – Chad Jun 02 '17 at 17:24

2 Answers2

9

Use braces and quote the name.

$info->{'im:name'}->label

You can see an example of this in the documentation.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • 1
    @Chad, also note you can pass a truthy value as the second argument to `json_decode()` and you'll get back an associative array instead of a stdClass object. Then you can reference `$info['im:name']`. – Alex Howansky Jun 02 '17 at 17:01
1

You should use braces, then put the key as string :

echo $info->{"im:name"}->label;
Ralph John Galindo
  • 1,190
  • 6
  • 11