We enter data in JSON
$file = file_get_contents('./data.json'); $formdata = json_decode($file,TRUE); unset($file); $formdata []= array( 'title' => $title, 'data' => $data, 'author' => $author, 'message' => $message ); file_put_contents('./data.json',json_encode($formdata)); unset($formdata); Get the array:
[{"title":"1","data":"1","author":"1","message":"1"},{"title":"12","data":"12","author":"12","message":"12"},{"title":"123","data":"123","author":"123","message":"123"}] When deleting a single object:
$filedata = json_decode(file_get_contents('./data.json'), true); $index = array_count_values([$_POST['index']]); $array_number = key($index); unset($filedata[$array_number]); file_put_contents('./data.json', json_encode($filedata)); besides deletion, the array is "transformed" into
{"0":{"title":"1","data":"1","author":"1","message":"1"},"2":{"title":"123","data":"123","author":"123","message":"123"}} and after that I can’t add more elements to it (the deletion is normal).
How to make it so that when deleting elements of an array, it does not change the view, but simply removes one element of the array? An example (it is necessary to look like an array after deleting [1] -th element):
[{"title":"1","data":"1","author":"1","message":"1"},{"title":"123","data":"123","author":"123","message":"123"}]