I have an array of videos Ids and when a user clicks on a button the videoId of the clicked video appends to the array.
When the user clicks again on the same button, the code search for the videoId and if it is found, the video ID is deleted from the array.
The array is saved on a json file and its formatted as ["aaa","bob"...]
however, when I delete a value from the array the json file is transformed to {"1":"aaa", "2":"bbb"...}
How can I prevent this from Happening?
PHP:
$NameFileJSON = $_GET["NameFile"];
$VideoId = $_GET["VideoId"];
$removeVideoId = $_GET["RemoveVideoId"];
$results = array($VideoId);
SAVE VIDEOID:
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);
}
}
DELETE VIDEOID:
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)