Question:
I get a strange behavior when I remove data from my array with unsetAnswer:
That’s because php unset doesn’t reduce the length of the array, it just empty the given key.Since it doesn’t change the length, whe. You do
$array[] = something
you continue incrementing the index, creating the 2 and 3 index you are seeing.When you do
json_encode
, since the keys doesn’t start from zero (or aren’t contiguous), it need to represent this array as an object.Exemplifying:
Solution
You can still use
unset()
, but you need to get only array values using the array_values()
function:If you have better answer, please add a comment about this, thank you!