我使用 jQuery 制作了一个小应用程序datepicker
。我从 JSON 文件中设置不可用的日期,如下所示:
{ "dates": ["2013-12-11", "2013-12-10", "2013-12-07", "2013-12-04"] }
我想检查给定的日期是否已在此列表中,如果是,请将其删除。我当前的代码如下所示:
if (isset($_GET['date'])) //the date given
{
if ($_GET['roomType'] == 2)
{
$myFile = "bookedDates2.json";
$date = $_GET['date'];
if (file_exists($myFile))
{
$arr = json_decode(file_get_contents($myFile), true);
if (!in_array($date, $arr['dates']))
{
$arr['dates'][] = $_GET['date']; //adds the date into the file if it is not there already
}
else
{
foreach ($arr['dates'] as $key => $value)
{
if (in_array($date, $arr['dates']))
{
unset($arr['dates'][$key]);
array_values($arr['dates']);
}
}
}
}
$arr = json_encode($arr);
file_put_contents($myFile, $arr);
}
}
我的问题是,在我再次对数组进行编码后,它看起来像这样:
{ "dates": ["1":"2013-12-11", "2":"2013-12-10", "3":"2013-12-07", "4":"2013-12-04"] }
有没有办法在 JSON 文件中找到日期匹配并将其删除,而编码后不会出现密钥?