-1

I was wondering if there was a way to retrieve a value of a php variabile in a JSON file.
For example, I have this following JSON file (file.json):

{
   "Hello": "Hello $name"
}

And this PHP file:

<?php
$name = "Rick";
$jsonFile = json_decode("file.json", true);
$hello = $jsonFile["Hello"];
echo $hello;
?>

What if I want to echo Hello Rick, instead of Hello $name?
Is it impossibile or is there a way to do it?

Seba
  • 7
  • 2
  • 1
    you need to evaluate a template string (that usually it's straightforward if defined as a template literal inside php itself) `strtr($hello, ['$name' => $name])` https://www.php.net/strtr – Diego D Jul 01 '22 at 09:53
  • I keep looking for duplicates, but in every page that I find, EVERYONE knows to read the file contents before trying to parse the json. – mickmackusa Jul 01 '22 at 10:18
  • This question is asking multiple questions. It could have also been closed as Needs More Focus, but I prefer to provide a close reason which helps the asker to move forward. – mickmackusa Jul 01 '22 at 11:13

3 Answers3

0

You can't use variable in json file. You should store the 'Rick' in json file or add 'Rick' after from taking json file.

$jsonFile=file_get_contents("file.json");
$json = json_decode($jsonFile, true);

In addition you can do it.

$json['Hello'] = str_replace('$name','Rick',$json['Hello']);
  • 1
    This low value question did not need to be answered to be resolved. Please flag basic questions for closure instead of answering when Stack Overflow already has pages that provide resolving advice. – mickmackusa Jul 01 '22 at 10:22
-1

One of the most easiest way is, you can code in this way to get the file content:

<?php
    $jsonFile = file_get_contents(base_path('path/to/file.json'));
    $jsonFile = json_decode($jsonFile);
?>

Now, $jsonFile can have all the file content. You can access the json content and get variables like $jsonFile["Hello"];

Talha Maqsood
  • 195
  • 1
  • 13
  • where is that json_decode how is that replacing `$name` with `Rick` – angel.bonev Jul 01 '22 at 09:59
  • You dont need to decode it because you are directly fetching the file.json content and $jsonFile holds the json. – Talha Maqsood Jul 01 '22 at 10:02
  • So https://www.php.net/manual/en/function.file-get-contents.php `Reads entire file into a string` The documentation is wrong? It is returning what ever you want . Not only string ? – angel.bonev Jul 01 '22 at 10:07
  • Yes my bad, it returns the string we need to decode this after fetch content from file – Talha Maqsood Jul 01 '22 at 10:10
  • And still how is that `$name` going to become `Rick` – angel.bonev Jul 01 '22 at 10:14
  • 1
    This low value question did not need to be answered to be resolved. Please flag basic questions for closure instead of answering when Stack Overflow already has pages that provide resolving advice. – mickmackusa Jul 01 '22 at 10:22
-2

I think you want to make an output for a json file.

<?php 
$name = "Rick";
$json['hello']="Hello $name";
$jsonfile = json_encode($json);
$path = "/pathofdirectorytosave/";
if (!file_exists($path)) {
    $myfile = fopen($path, 'w');
}
file_put_contents($path, $jsonfile);
fclose($myfile);
?>