I have made a small application using jQuery datepicker
. I am setting unavailable dates to it from json file which looks like this:
我使用jQuery datepicker做了一个小应用程序。我从json文件中设置了不可用日期,如下所示:
{"dates":["2013-12-11","2013-12-10","2013-12-07","2013-12-04"]}.
I would like to check if a date given is already in this list and remove it if so. My current code looks like this:
我想检查一个给定的日期是否已经在这个列表中,如果是,我想删除它。我当前的代码是这样的:
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);
}
My problem here is that after I encode the array again, it looks like this
我的问题是,当我再次对数组进行编码后,它看起来是这样的。
{"dates":["1":"2013-12-11","2":"2013-12-10","3":"2013-12-07","4":"2013-12-04"]}
Is there a way to find the date match in the json file and remove it, w/o the keys appearing after the encode?
是否有一种方法可以找到json文件中的日期匹配项并将其删除,将在编码后出现的键删除?
Any help is appreciated.
任何帮助都是感激。
2 个解决方案
#1
49
Use array_values()
for your issue:
使用array_values()解决您的问题:
$arr['dates'] = array_values($arr['dates']);
//..
$arr = json_encode($arr);
Why? Because you're unsetting array's key without re-ordering it. So after this the only way to keep that in JSON will be encode keys too. After applying array_values()
, however, you'll get ordered keys (starting from 0
) which can be encoded properly without including keys.
为什么?因为你在没有重新排序的情况下解除了数组的键。在此之后,保持JSON格式的唯一方法就是对键进行编码。然而,在应用array_values()之后,您将获得有序键(从0开始),可以在不包含键的情况下正确编码这些键。
#2
1
You are ignoring the return value of array_values
in your existing attempt to reindex the array. Correct is
在重新索引数组的现有尝试中,您将忽略array_values的返回值。正确的是
$arr['dates'] = array_values($arr['dates']);
The reindexing should also be moved outside the foreach
loop, there is no point in reindexing multiple times.
驯鹿化也应该移到foreach循环之外,多次驯鹿化是没有意义的。
#1
49
Use array_values()
for your issue:
使用array_values()解决您的问题:
$arr['dates'] = array_values($arr['dates']);
//..
$arr = json_encode($arr);
Why? Because you're unsetting array's key without re-ordering it. So after this the only way to keep that in JSON will be encode keys too. After applying array_values()
, however, you'll get ordered keys (starting from 0
) which can be encoded properly without including keys.
为什么?因为你在没有重新排序的情况下解除了数组的键。在此之后,保持JSON格式的唯一方法就是对键进行编码。然而,在应用array_values()之后,您将获得有序键(从0开始),可以在不包含键的情况下正确编码这些键。
#2
1
You are ignoring the return value of array_values
in your existing attempt to reindex the array. Correct is
在重新索引数组的现有尝试中,您将忽略array_values的返回值。正确的是
$arr['dates'] = array_values($arr['dates']);
The reindexing should also be moved outside the foreach
loop, there is no point in reindexing multiple times.
驯鹿化也应该移到foreach循环之外,多次驯鹿化是没有意义的。