PHP-JSON在取消设置值时以错误格式保存
我有一个视频ID数组,当用户单击按钮时,所单击视频的视频ID会附加到该数组中 当用户再次单击同一按钮时,代码将搜索视频ID,如果找到,则视频ID将从阵列中删除 数组保存在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 != "")&&(
[“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
。