Vis
Vis

Reputation: 41

Using dynamic value in JSON

I have a json file (myJson.json) in which I am adding a dynamic value

[
  {
    "menu":"<?php echo $r; ?>",
  }
]

Now I am accessing this JSON in my Php file as :

$r="HOME";
$str_data = file_get_contents("MyJson.json");
$data = json_decode($str_data,true);
var_dump($data[0]['menu']);


output:"<?php echo $r; ?>"
Is there any way to get "HOME" as an output?

Upvotes: 0

Views: 116

Answers (2)

JustCoding
JustCoding

Reputation: 71

Can you just keep placeholder in your JSON and then substitute it with valid value? Like, [ { "menu":"@@HOME@@", } ]

Then string replace that string with $r.

Upvotes: 0

senK
senK

Reputation: 2802

You may try like

[
  {
    "menu":"r",
  }
]

and is json

$r="HOME";
var_dump($$data[0]['menu']);

You will get home as an output

Upvotes: 2

Related Questions