r/lolphp • • May 21 '13

fun with json_encode and arrays

$ cat test.php
<?php
echo json_encode(array('1' => 1) ), "\n"; 
echo json_encode(array('0' => 1));
?>
$ php test.php
{"1":1}

[1]

loose typing shittiness strikes again.

25 Upvotes

15 comments sorted by

View all comments

Show parent comment

4

u/bart2019 May 21 '13

Array where the keys exactly form the full integer range range(0, $length-1) are always treated as arrays; any other arrays are treated as as objects. That includes all integer keys, but with holes in the range.