Hi everyone I made a plugin that can lock/unlock doors, I save all the doors in a JSON file. This is what the JSON file normally looks like: Spoiler: JSON code Code: { [ { "key_name": "Gemeentehuis", "coords": { "x": 265, "y": 13, "z": 286 } }, { "key_name": "Gemeentehuis", "coords": { "x": 261, "y": 13, "z": 296 } }, { "key_name": "Gemeentehuis", "coords": { "x": 262, "y": 12, "z": 294 } }, { "key_name": "Gemeentehuis", "coords": { "x": 261, "y": 13, "z": 295 } } ] } But whenever I remove something from the JSON file, with this code: The code basically checks if the touched block is the same as the block in the JSON file, if it is the same it will remove it and update the JSON file. Spoiler: PHP Code PHP: if(isset($this->unlockSession[$player->getName()])){ if($this->unlockSession[$player->getName()]){ $event->setCancelled(); $x = $event->getBlock()->getX(); $y = $event->getBlock()->getY(); $z = $event->getBlock()->getZ(); $json_file = (array) json_decode(file_get_contents($this->path, true)); $index = 0; foreach ($json_file as $value) { $x_j = $value->coords->x; $y_j = $value->coords->y; $z_j = $value->coords->z; var_dump(array($x_j, $y_j, $z_j)); if ($x == $x_j && $z == $z_j) { if(abs($y - $y_j) <= 1 || abs($y_j - $y) <= 1) { break; } } $index += 1; } var_dump($json_file); echo "------------------------------------\n"; var_dump($json_file[$index]); echo "------------------------------------\n"; unset($json_file[$index]); var_dump($json_file); $new_json = json_encode($json_file, JSON_PRETTY_PRINT); file_put_contents($this->path, $new_json); $player->sendMessage("§aDe vergrendeling is opgeheven!"); unset($this->unlockSession[$player->getName()]); } } } Now after I run this code, my JSON file turns like this: Spoiler: JSON code after Code: { "1": { "key_name": "Gemeentehuis", "coords": { "x": 261, "y": 13, "z": 296 } }, "2": { "key_name": "Gemeentehuis", "coords": { "x": 262, "y": 12, "z": 294 } }, "3": { "key_name": "Gemeentehuis", "coords": { "x": 261, "y": 13, "z": 295 } } } Suddenly there appears "1", "2" and "3". I don't know from where this comes or why it is there. Instead I expect this to happen: Spoiler: Expected JSON code Code: [ { "key_name": "Gemeentehuis", "coords": { "x": 261, "y": 13, "z": 296 } }, { "key_name": "Gemeentehuis", "coords": { "x": 262, "y": 12, "z": 294 } }, { "key_name": "Gemeentehuis", "coords": { "x": 261, "y": 13, "z": 295 } } ] You can see my whole code here: https://hastebin.com/azoharuhok.xml I hope someone can help!
maybe try using associative arrays and serialising it when saving to the file and when reading it just unserialise it
so something like: PHP: $keys = [];$temp = array( 'KeyName' => 'exampleKeyName', 'x' => 1, 'y' => 2, 'z' => 3);array_push($keys, $temp);var_dump($keys);//echo $temp['KeyName']; Every time a new lock is made push an associative array into $keys
The problem is that you are using unset, which will remove a value from the array, but not reorder the indices. See this example: Spoiler: output PHP: <?php$arr = ["hello", "this", "isatest"];var_dump($arr);unset($arr[0]);var_dump($arr); will output Code: array(3) { [0]=> string(5) "hello" [1]=> string(4) "this" [2]=> string(7) "isatest" } array(2) { [1]=> string(4) "this" [2]=> string(7) "isatest" } As you can see, the array now starts at one. If you will then json encode it, in order to preserve that order, the json encoder will put those indices there. You could use array_values() before serialising it to json, or use array_splice() instead of unset. See this Stackoverflow post for more information.
Hi, thank you very much! I now understand why it didn't work and I fixed the problem. Have a nice day
Just a moderator note: next time please edit the message if you have more information to add, rather than sending a new message