Hi i wonder how i can delete data from json file based on id if my json struct looks like that:
[
{
"id": 1,
"title": "a",
"decription": "b"
},
{
"id": 2,
"title": "c",
"decription": "d"
}
]
What I've tried so far:
if (isset($_POST['delete_post']))
{
$id = $_POST['post-id'];
if(empty($id)) return;
$posts = json_decode(file_get_contents('posts.json'));
foreach ($posts as $post)
{
if ($post->id == $id)
{
unset ($post);
}
$save = json_encode($posts, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
file_put_contents('posts.json', $save);
}
}
And i literally stuck at this ponint.