-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Add Arr::isList()
method
#39277
Conversation
Can you give an example where |
@taylorotwell The RFC describes cases where obvious implementation (which is used in
|
|
That's what happens when you read code tired -_- One thing to consider would be performance for huge arrays, and without tests I can't even give a guess which would be faster. Probably if array is million of elements long and last key is wrong then array_keys would be faster. |
@PHLAK would it be better to just convert this method to |
@taylorotwell I assume you meant |
Arr::isList()
methodArr::isList()
method
Added the
Arr::isList()
method for determining if an array is a "list". An array is a "list" if all array keys are sequential integers starting from 0 with no gaps in between.This is a polyfilled version of the
array_is_list()
function that is coming to PHP with v8.1 (currently unreleased). Additionally, Laravel 8 supports PHP versions back to 7.3 so this PR brings this functionality to applications still on these versions.Implementation
This implementation uses the polyfill code as described in the RFC proposal which avoids potential issues when using other methods (e.g.array_values($array) === $array
) for determining "listness".Future Considerations
In the future this method can be refactored to use the native
array_is_list()
function when support for PHP versions < 8.1 are dropped. Alternatively the method may be removed all together.