0

So hello, I am working with json data and created a admin.php page thats adds data to the database . So before I took about the question I will show you my code. url.json

{
  "minecraft" : [
    {
      "version" : "MC v1",
      "url" : "http://www.google.com"
    },
    {
      "version" : "MC v12",
      "url" : "http://www.google.com"
    }
    ]
}

admin.php code snippet

 ## Add MC to version
    $Version = "MC " . strtolower($_POST["version"]);
    ## Insert Data To JSON file!!!!
    $Data_Now = file_get_contents('url.json');
    $json = json_decode($Data_Now, true);
    $DataAppend = array(
      "version" => $Version,
      "url" => $_POST["url"]
    );
    $json[] = $DataAppend;
    $final_data = json_encode($json);
    if(file_put_contents('url.json', $final_data)) {
      # Displays message here
    }

So here is my Question As you see in my json file I have an array called minecraft and I am trying to insert new data into that array but php won't do it . instead it puts it outside of the array I tried to insert the array name in the variable $json but it will break the json file completely .

How do I fix it?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
KTK27
  • 11
  • 3
  • Add it not to the overall array, but to the entry in that overall array you want to add it to: `$json["minecraft"][] = $DataAppend;`. This is rather basic array access, just FYI. – hakre Apr 29 '18 at 10:49
  • Thanks hakre for the answer !! Sorry to be bad at php – KTK27 Apr 29 '18 at 10:54
  • Not bad, just wanted to give you a pointer which information might have been missing to you. – hakre Apr 29 '18 at 10:55

0 Answers0