How can I re-index a JSON after using unset($json -> nodes[$data_deleteNode])
to remove an entry? The common method array_values($json)
, which usally close the gaps doesn´t work. I understand the error message but do not have an idea to solve it.
array_values() expects parameter 1 to be array, object given
There is a similar thread which refers to the solutions I tried - PHP json_encode as object after PHP array unset() the only difference is the JSON.
If I do not perform any sorting, unset() adds an index which cause my JSON to be not readable for other scripts.
The affected PHP:
<?php
header("Access-Control-Allow-Origin: *");
$data_deleteNode = ($_POST['id']);
$json = file_get_contents("data2.json");
$json = json_decode($json);
//$data_deleteNode = json_decode($data_deleteNode);
//array_splice($json, 3, 1);
printf("%s\n", json_encode($json));
unset($json -> nodes[$data_deleteNode]);
printf("%s\n", json_encode($json));
$json = array_values($json);
$json = json_encode($json);
file_put_contents("data2.json", $json)
?>
UPDATE: modified playground which shows the error https://3v4l.org/XPbuo#v730