Answer the question
In order to leave comments, you need to log in
How to properly write data to JSON?
Hello, I just can’t understand how to write data to a JSON file so that they can be read correctly later, tell me where I made a mistake?
PHP code itself:
$user = "user213";
$token = "token213";
$d1 = array("user" => "$user", "token" => "$token");
$data[0] = $d1;
$inp = file_get_contents('tokens.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('tokens.json', $jsonData);
[
[{
"user": "user212",
"token": "token212"
}],
[{
"user": "user213",
"token": "token213"
}]
]
{
"users":
[
{
"user": "user1",
"token": "token1"
},
{
"user": "user2",
"token": "token2"
}
]
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question