first time poster here having a fair bit of trouble with something that may not be that complicated to fix. Firstly, I have extremely little experience with PHP, so I do apologize if I'm missing something obvious or if I ask for further clarification. Everything so far has been client side with HTML/JavaScript.
I'm looking to incorporate a registration system into my application. It's for a physiotherapist program where users details are stored within a JSON file. (I understand there are better alternatives).
The JSON contents currently is in this format:
{"users":[
{
"userid": "0",
"password":"template",
"physio": "[email protected]",
"firstName":"template",
"lastName":"template",
"exerciseregime": {
"exercisenumber":"5",
"exercises":["2","1", "3", "5", "4"]
}
},
{
"userid": "1",
"password":"user",
"physio": "[email protected]",
"firstName":"User",
"lastName":"Physio",
"exerciseregime": {
"exercisenumber":"1",
"exercises":["2"]
}
},
And so forth. What I'm looking to do is use the details taken from the form and add them to the existing users array in this format.
The code I am currently using is
<?php
$file = "userjson.json";
$FirstName = $_POST["FirstName"];
$Surname = $_POST["Surname"];
$Password = $_POST["Password"];
$Email = $_POST["email"];
$UserID = $_POST["UserID2"];
$json = json_decode(file_get_contents($file), true);
$json["firstName"] = $FirstName;
$json["lastName"] = $lastName;
$json["password"] = $Password;
$json["physio"] = $Email;
$json["UserID"] = $UserID;
file_put_contents($file, json_encode($json));
?>
I used a previous answer found on the website to achieve this. The details are saved to the JSON file except not in the correct format. Rather than adding to the existing array as a new user, a new object is appended after the array rather than inside it. How would I go about fixing this and creating a new "user" inside the user array?
Also, a second question but not as important: How would I go about writing an array inside of this new object? I'm having a slight struggle in trying to add the "Exerciseregime" array to the object.
I would be extremely grateful for your help, and I am thankful for your consideration.