0

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
  • add echo $json_data; before writing to file Commented Apr 23, 2017 at 6:45

1 Answer 1

2

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

also it will be better to deal with pure arrays then encode your whole final array, $array = json_encode(['detailsjson' => $var]);

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.