-1

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)
SNos
  • 3,430
  • 5
  • 42
  • 92

4 Answers4

0

json_encode turns array into an object use json_encode flags to force it to an array

Unsetting index in array turns it to an object ensure it's not an assoc array after using unset

Community
  • 1
  • 1
Barry
  • 362
  • 3
  • 14
0

json_encode will only produce array notation if the array indexes are sequental numbers starting from 0. When you use unset(), it creates a gap in the indexes, so it's sent as an object to maintain the indexes. For this reason, unset() should generally only be used with associative arrays.

Use array_splice() rather than unset() to remove an element from the array, and then all the indexes after it will be shifted down.

array_splice($arr, $index, 1);
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

same @Barry said, if you still want save as array in json, you can use this

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)

see more at http://php.net/manual/en/function.array-values.php

Mr Jerry
  • 1,686
  • 3
  • 14
  • 22
0
  1. You can replace your unset with array_splice:
    array_splice($array, $i, 1);
  2. You can reset indexes with array_values:
    json_encode(array_values($array));
vp_arth
  • 14,461
  • 4
  • 37
  • 66