3 ms·
I'm kinda used to PHP "arrays", but I really wish it had proper lists. This array_is_list function doesn't change things much: I already have the same implement
by krick 6y ago
I'm kinda used to PHP "arrays", but I really wish it had proper lists. This array_is_list function doesn't change things much: I already have the same implemented in many of my projects. The point is that I shouldn't do that: I just should use a typehint the same way as I'd do it with array or MyCustomClass.
Of course there is Ds\Sequence, but having it as a part of some relatively obscure library isn't the same as having it as a part of a language, with syntax as simple as [1,2,3]. Actually, I'm not even sure: does it have as good performance, as "arrays" do? I never tested.
- duskwuff 6y agoWhat `array_is_list()` changes is that it makes this check fast. Previously, the fastest method of checking for a list-like array was `$arr == array_values($arr)`, which was slow on large lists and could fail entirely on some unusual structures containing circular references. (I was involved in the development of this function: https://github.com/php/php-src/pull/4886 https://github.com/php/php-src/pull/4886)
- tyingq 6y agoIs array_is_map(), etc, planned? Or is the pure php equivalent fast enough for those?
- duskwuff 6y agoYou mean `!array_is_list()`? As far as PHP is concerned, a list is a subset of arrays where the keys are ordered integers starting at zero. Any array that doesn't fit this pattern could be considered a "map" -- there's no middle ground.