【发布时间】:2018-10-14 06:15:48
【问题描述】:
您好,如果我的 json 结构看起来像这样,我想知道如何根据 id 从 json 文件中删除数据:
[
{
"id": 1,
"title": "a",
"decription": "b"
},
{
"id": 2,
"title": "c",
"decription": "d"
}
]
到目前为止我已经尝试过:
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);
}
}
我确实卡在了这个节点上。
【问题讨论】:
-
嗨,您需要展示一些努力/代码,然后人们会提供帮助!也许先尝试将其放入数组中?
-
哎呀,我的错误我在没有 PHP 代码的情况下保存了,更新了。
-
对我来说看起来不错。我会试试看。你只是得到一些语法错误吗?检查日志。概念步入正轨。
-
不,没有收到任何错误,但它根本不会删除任何内容
标签: php