I have my json echo working
echo '{"detailsjson":'.json_encode($var).'}';
Now I want to write this to a file on server example output.json
I use the php code
$json_data = {"detailsjson":.json_encode($var).};
file_put_contents('output.json', $json_data);
i don't get the file, but for
file_put_contents("output.json", json_encode($var));
it work. I want to include detailsjson in my json so that i can get
{"detailsjson":[{"Name":"Amateur : Paul Smith",.....
Thanks
splash58
26.2k3 gold badges25 silver badges35 bronze badges
asked Apr 23, 2017 at 6:36
1 Answer 1
Your $json_data
variable is not "valid". You have to put everything in single quotes to make it work (not in double quotes because you use them in your json). Like this:
$json_data = '{"detailsjson":'.json_encode($var).'}';
Update (see comment from Hassan):
It is better to do this with pure arrays.
$json_data = json_encode(['detailsjson' => $var]);
You can compare them by speed here (using a string) and here (using pure arrays).
answered Apr 23, 2017 at 6:45
Sign up to request clarification or add additional context in comments.
1 Comment
hassan
also it will be better to deal with pure arrays then encode your whole final array,
$array = json_encode(['detailsjson' => $var]);
lang-php
echo $json_data;
before writing to file