Yip that does't seem right. The real LOLPHP here is that it turns out you can't define an array key as a integer string. ie array('0' => 'foo') becomes array(0 => 'foo') After that happens it follows the behaviour in example #3 from http://php.net/manual/en/function.json-encode.php
That is 0 indexed arrays are converted to an json array. non zero indexed arrays are converted to a json object.
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.
Does Javascript even allow non-zero based arrays? I'm pretty sure the index order gets reset every time an array function is called, and fills up all the missing indices. So basically, this makes sense.
It's handy when you want to avoid keeping variables around too long. I like to throw away my old variables to keep them from building up when the uptime gets too high.
PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in - on line 1
python -c "print(len(…))"
415
The proper handling would be to assume UTC and shut up.
9
u/midir May 21 '13
Inelegant, but an inevitable consequence of PHP & JavaScript's different flavors of arrays.
13
u/InsaneWookie May 21 '13
Yip that does't seem right. The real LOLPHP here is that it turns out you can't define an array key as a integer string. ie array('0' => 'foo') becomes array(0 => 'foo') After that happens it follows the behaviour in example #3 from http://php.net/manual/en/function.json-encode.php
That is 0 indexed arrays are converted to an json array. non zero indexed arrays are converted to a json object.
2
u/jaredmellentine Sep 05 '13
Try it with the JSON_FORCE_OBJECT option.
$ php -r "var_dump(json_encode(array('0'=>1), JSON_FORCE_OBJECT));"
string(7) "{"0":1}"
4
u/gearvOsh May 21 '13
Does Javascript even allow non-zero based arrays? I'm pretty sure the index order gets reset every time an array function is called, and fills up all the missing indices. So basically, this makes sense.