Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-JSON在取消设置值时以错误格式保存_Php_Arrays_Json - Fatal编程技术网

PHP-JSON在取消设置值时以错误格式保存

PHP-JSON在取消设置值时以错误格式保存,php,arrays,json,Php,Arrays,Json,我有一个视频ID数组,当用户单击按钮时,所单击视频的视频ID会附加到该数组中 当用户再次单击同一按钮时,代码将搜索视频ID,如果找到,则视频ID将从阵列中删除 数组保存在json文件中,其格式为[“aaa”,“bob”…],但是,当我从数组中删除一个值时,json文件将转换为{“1”:“aaa”,“2”:“bbb”…} 我怎样才能防止这种情况发生 PHP: 保存视频ID: if (($NameFileJSON != "")&&($VideoId != "")&&(

我有一个视频ID数组,当用户单击按钮时,所单击视频的视频ID会附加到该数组中

当用户再次单击同一按钮时,代码将搜索视频ID,如果找到,则视频ID将从阵列中删除

数组保存在json文件中,其格式为
[“aaa”,“bob”…]
,但是,当我从数组中删除一个值时,json文件将转换为
{“1”:“aaa”,“2”:“bbb”…}

我怎样才能防止这种情况发生

PHP:

保存视频ID:

if (($NameFileJSON != "")&&($VideoId != "")&&($removeVideoId == "")){

        $filename = "json/likesJSON/$NameFileJSON.json";

        if (file_exists($filename)) {

            echo "The file $filename exist";

            $inp = file_get_contents("json/likesJSON/$NameFileJSON.json");
            $arr = json_decode($inp);

            array_push($arr, $results[0]);

            $fp_login = fopen("json/$NameFileJSON.json", w);
            fwrite($fp_login, json_encode($arr));
            fclose($fp_login);

        } else {

             echo "The file $filename does not exist";
            $fp_login = fopen("json/$NameFileJSON.json", w);
            fwrite($fp_login, json_encode($results));
            fclose($fp_login);

        }
if (($NameFileJSON != "")&&($VideoId == "")&&($removeVideoId != "")){

    $inp = file_get_contents("json/$NameFileJSON.json");
            $arr = json_decode($inp);

    if (($index = array_search($removeVideoId, $arr)) !== false) {
        echo  $index;
        unset($arr[$index]);
    }

            $fp_login = fopen("json/$NameFileJSON.json", w);
            fwrite($fp_login, json_encode($arr));
            fclose($fp_login);      

}

print_r(json_encode($arr)
}

删除视频ID:

if (($NameFileJSON != "")&&($VideoId != "")&&($removeVideoId == "")){

        $filename = "json/likesJSON/$NameFileJSON.json";

        if (file_exists($filename)) {

            echo "The file $filename exist";

            $inp = file_get_contents("json/likesJSON/$NameFileJSON.json");
            $arr = json_decode($inp);

            array_push($arr, $results[0]);

            $fp_login = fopen("json/$NameFileJSON.json", w);
            fwrite($fp_login, json_encode($arr));
            fclose($fp_login);

        } else {

             echo "The file $filename does not exist";
            $fp_login = fopen("json/$NameFileJSON.json", w);
            fwrite($fp_login, json_encode($results));
            fclose($fp_login);

        }
if (($NameFileJSON != "")&&($VideoId == "")&&($removeVideoId != "")){

    $inp = file_get_contents("json/$NameFileJSON.json");
            $arr = json_decode($inp);

    if (($index = array_search($removeVideoId, $arr)) !== false) {
        echo  $index;
        unset($arr[$index]);
    }

            $fp_login = fopen("json/$NameFileJSON.json", w);
            fwrite($fp_login, json_encode($arr));
            fclose($fp_login);      

}

print_r(json_encode($arr)
使用json_编码标志将其强制到数组


确保在使用unset后它不是assoc数组

json\u encode
仅当数组索引是从
0
开始的序列号时才会生成数组表示法。使用
unset()
时,它会在索引中创建一个间隙,因此会将其作为对象发送以维护索引。因此,
unset()
通常只应与关联数组一起使用

使用
array\u splice()
而不是
unset()
从数组中删除元素,然后将该元素之后的所有索引下移

array_splice($arr, $index, 1);

同样@Barry说,如果您仍然想要json中的另存为数组,您可以使用这个

if (($NameFileJSON != "")&&($VideoId == "")&&($removeVideoId != "")){

    $inp = file_get_contents("json/$NameFileJSON.json");
            $arr = json_decode($inp);

    if (($index = array_search($removeVideoId, $arr)) !== false) {
        echo  $index;
        unset($arr[$index]);
    }
    $arr = array_values($arr);

            $fp_login = fopen("json/$NameFileJSON.json", w);
            fwrite($fp_login, json_encode($arr));
            fclose($fp_login);      

}

print_r(json_encode($arr)
更多信息请访问

  • 您可以将
    未设置的
    替换为
    阵列拼接

    array\u拼接($array,$i,1)
  • 您可以使用
    数组\u值重置索引

    json_编码(数组_值($array))

  • 您是否尝试在JavaScript中使用
    Array.splice()
    而不是
    delete
    ?或者,你所说的删除是什么意思?它是php端的行为
    array\u values
    在这里会有所帮助。第一个链接中的答案是如何在
    json\u decode
    中使用标志,而不是
    json\u encode