【发布时间】:2013-12-20 19:10:16
【问题描述】:
我使用 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 文件中找到日期匹配项并将其删除,而密钥不会出现在编码之后?
【问题讨论】:
-
看起来不像,而是像这样:
{ "dates": {"1":"2013-12-11", "2":"2013-12-10", "3":"2013-12-07", "4":"2013-12-04"} }。 diff:花括号包裹一个对象