What happens when you use
unset($json_arr) is that the first element is removed, but the keys are not updated. If you inspect the array after the removal, you'll find that your array has two elements, at
When you then perform a
json_encode($json_arr) on this, PHP's JSON decoder looks at the array and since arrays are supposed to begin at the
0th element but this array begins at
1, it decides that in order to preserve the keys, the array would have to be converted to an associative array - which transforms the integer array keys into string keys in JSON.
For a short and quick solution, you can try:
$json_arr = array_diff($json_arr, [$key]);
You could even use
array_values - see here for inspiration.