-7

I didn't know what to write about the title of the problem:) I want to add a string to the query but I get internal 500 error

$arr = json_decode($json, true);
$name = "Peter";
echo $arr[$name];  // Output: 65

It's correct but,

$obj = json_decode($json);
echo $obj->$name;

not working.

Of course this is not normal code but caybe this could be a method?

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

Given a json string {"name":"foo"} You can display name by writing

$json = '{"name":"foo"}';
$data = json_decode($json);
echo $data->name ;

Since name is a property of object $data .

if you'd write $data->$name , it would mean you have a $name variable somewhere you wanted to refer.

mpm
  • 20,148
  • 7
  • 50
  • 55