1

I'm trying to access part of an StdClass that has a property #text. PHP uses '#' for comments so I'm having trouble getting PHP to parse it not as a comment. An example of the StdClass is below

stdClass Object ( [artist] => stdClass Object ( [name] => John Denver [mbid] => 34e10b51-b5c6-4bc1-b70e-f05f141eda1e [url] => http://www.last.fm/music/John+Denver [image] => Array ( [0] => stdClass Object ( [#text] => http://userserve-ak.last.fm/serve/34/521025.jpg [size] => small

and I've tried to access it with:

$json->artist->image[0]->#text

but how can I escape the '#' or tell php to interpret it differently. Or is there another format to find the #text property.

I also tried:

$json['artist']['image'][0]['#text']

but I get an error. I'm sure this is something simple but it's really got me at the moment.

James Billings
  • 448
  • 7
  • 16
  • possible duplicate of [How do I access a PHP object attribute having a dollar sign?](http://stackoverflow.com/questions/2093169/how-do-i-access-a-php-object-attribute-having-a-dollar-sign) – mario Jun 05 '12 at 11:44
  • 1
    Second parameter for json_decode() set to TRUE. Did you try that? – mario Jun 05 '12 at 11:45

3 Answers3

4

You can access such properties with the following code:

$object->{'#text'}
Rawkode
  • 21,990
  • 5
  • 38
  • 45
0

It seems to be JSON data, so when decoding, you can also set the second parameter to true:

$json = json_decode($input, true);

Now you are able to use this:

$json['artist']['image'][0]['#text']
Jeroen
  • 13,056
  • 4
  • 42
  • 63
0

Citing another answer you can put the key in a variable an accessing it via this variable.

$var='myvarwithstrangecharacters$asdfas#';

$object->$var;//do whatever you want

from: How do I access a PHP object attribute having a dollar sign?

Community
  • 1
  • 1
jmlv21104
  • 89
  • 12