One interesting difference I found is to use json_encode .
json_encode(array(0=>0,1=>1,2=>2)); > [0,1,2] json_encode(array(0=>0,2=>2)); > {"0":0,"2":2}
As a single example, this makes sense, but it is more surprising when combined, for example, with array_filter .
$f = function($x) { return $x != 1; }; json_encode(array_filter(array(0,1,2), $f)); > {"0":0,"2":2}
We started with a numerical array, filtered out some elements, but the resulting json is an associative array!
Note that we can get the desired json using array_values .
json_encode(array_values(array_filter(array(0,1,2),$f))); > [0,2]
ceyko
source share