This question already has an answer here:
- PHP json_encode - Strange behaviour 4 answers
I find a weird result of json_encode
today in my production code, sometimes it returns an array, like ["perl","php","python"]
, but sometimes an object, like {"0":"perl","2":"python"}
. Here is a minimal example:
$a = array("perl", "php", "python", "something other");
foreach($a as $index => $item)
{
if ($item == "something")
unset($a[$index]);
}
echo json_encode($a);
Why does this happen?
</div>