使用PHP从JSON中删除完整的日历事件

Remove fullcalendar event from JSON using PHP

本文关键字:日历 事件 删除 PHP JSON 使用      更新时间:2023-09-26

我想使用PHP从Fullcalendar json文件中删除事件。

例如,在我的json文件中有两个事件:

[{"id":1,"title":"TERMIN ZAREZERWOWANY","start":"2016-10-13T07:00","end":"2016-10-13T08:00"},{"id":2,"title":"TERMIN ZAREZERWOWANY","start":"2016-10-14T08:00","end":"2016-10-14T09:00"}]

当我点击事件时,JS从前端删除事件并触发以下PHP代码(现在我想删除第一个事件$array_data[0]):

if (file_exists('cal.json')) {
            $current_data = file_get_contents('cal.json');
            $array_data = json_decode($current_data, true);
            unset($array_data[0]);
            $new_data = json_encode($array_data);   
                file_put_contents('cal.json', $new_data);
        }
        else
        {
            $error = 'json not exist';
        }

之后,我的新json文件看起来像:

{"1":{"id":2,"title":"TERMIN ZAREZERWOWANY","start":"2016-10-14T08:00","end":"2016-10-14T09:00"}}

我做错了什么?{"1"是什么?{…在新的json文件?请帮忙,抱歉我的英文不好;)

当您unset时,您正在取消该数组键的值,而不是删除键。

请使用array_shiftarray_pop,它们将分别从数组的开头或末尾删除该元素。

$current_data = file_get_contents('cal.json');
$array_data = json_decode($current_data, true);
array_shift($array_data);
$new_data = json_encode($array_data);   
file_put_contents('cal.json', $new_data);