How to access an object of a PHP object with a $ symbol in its name?

I decode some JSON (from the Youtube data API) using json_decode, and it gives me an object that looks like this when var_dump () ed:

object(stdClass)[29]
  public 'type' => string 'text' (length=4)
  public '$t' => string 'Miley and Mandy! KCA VIDEO WINNERS' (length=34)

How can I access the $ t element?

+3
source share
3 answers

Try

$member = '$t';
$obj->$member
+7
source

You can use json_decode second argument

$data = json_decode($text, true);
echo $data['$t'];
+2
source

$ t will only be interpreted as a reference to a variable when used outside quotation marks or in double quotation marks ("$ t"). Single quoted strings ('$ t') are not parsed for variable references.

echo $data['$t'];

Will do what you want.

0
source

Source: https://habr.com/ru/post/1705661/


All Articles