Hacker News new | ask | show | jobs
by Navarr 1591 days ago
I love PHP "arrays" but there is one tiny gotcha that becomes more important as we do type-safety stuff more and more.

`$a['1'] = 'string';` becomes `$a[1] = 'string';`. This appears to be the case for any fully-fledged decimal number where `(string)(int)$key === $key`

So if you are expecting `array_keys` to return all strings, you might get surprised

1 comments

Some years ago I was appending underscores to key strings to avoid that. We're talking PHP 5 era. But even at that time stdClass was available. Today, I much prefer either using a stdClass object or actually making a real class when I reach for an associative array. They still have their use cases, though.