0

I have some JSON that looks like this:

{
    people: []
}

My Goal at the moment is to simply add elements to the array of people. But in the end I also would like to be able to remove the elements again.

I have tried something like:

<?

    $file = "test.json";
    $json = file_get_contents($file);
    $get = json_decode($json);

    array_push($get->people, $_GET['person']);

    file_put_contents($file, json_encode($get));

?>

However, this does not work. I'm pretty sure there is an easier solution for this. Thanks for any help.

Shawn31313
  • 5,978
  • 4
  • 38
  • 80

1 Answers1

0

You have invalid JSON. Keys must be quoted.

{"people": []}

Otherwise, your code should work. Although I would recommend not using PHP short open tags and filtering user input.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174