php 使用 json_encode() 时删除数组索引引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20372982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:07:37  来源:igfitidea点击:

Removing array index reference when using json_encode()

phpjqueryjson

提问by d.bikov

I have made a small application using jQuery's datepicker. I am setting unavailable dates to it from a 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, without the keys appearing after the encode?

有没有办法在 JSON 文件中找到日期匹配并将其删除,而不会在编码后出现密钥?

回答by Alma Do

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),这些密钥可以在不包含密钥的情况下正确编码。

回答by Jon

You are ignoring the return value of array_valuesin 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 foreachloop, there is no point in reindexing multiple times.

重新索引也应该移到foreach循环之外,多次重新索引没有意义。

回答by Amin Shojaei

In Laravel collections(just in case) you can do

Laravel 集合中(以防万一)你可以做

    $newArray = $collection->values()->toArray();

or

或者

    $jsonEncoded = $collection->values()->toJson();