Escaping # sign in a field name from API results

I have a small issue with the Json output from an API (it’s the last.fm API, if that’s relevant): a field name in the results includes a # sign and I’m not sure how to handle this.

For example, if $response->artist looks like this:

stdClass Object
(
    [mbid] => f271fb3e-a850-484a-8854-d335da7a713c
    [#text] => Ekin Fil
)

accessing the first part with $response->artist->mbid is no problem at all, but trying to access the artist name with $response->artist->#text throws an error because of the # sign.

Is there any way to handle this?

Working with an array instead of an object kind of solves this:

$request['artist']['#name']

but I would still be curious if there is any way to do that with the object.

I think

$request->artist->{'#name'};

should technically work. Don’t know if there’s a better way

1 Like

Thank you so much, this works!

1 Like