Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays
| From: | Josh Bruce | Date: | Tue, 22 Dec 2020 13:22:57 +0000 |
| Subject: | Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112590@lists.php.net to get a copy of this message | ||
> For example, with
>
> $a = ['foo', 'bar', 'baz’];
>
>
is_list() will return true, but
> if you run $a through asort(),
> is_list() will return false
> because the keys are no longer
> consecutive integers, but is there any doubt this is still a list?
> Maybe in a pure sense, it’s not, but I think this could be confusing.
>
tl;dr: Could add flags to is_array.
In the library I mentioned, if given a 0-indexed, sequentially keyed array (list), I would sort or
merge and pass the result to array_values()...return same type given of the three options (map,
list, dict).
But that might feel more like introducing a new type - rather than having a native way to check:
is_indexed_array(array $array[, int $flags = PHP_MAP])
SEQUENTIAL
SEQUENTIAL_DESC
STARTS_AT_0
is_associative_array(array $array)
or “array” could come first to create a triple set of is_array functions. Which brings me to
thinking a flag could ge added to is_array.
- no new functions to remember in the SL
- should be able to do in a non-BC way, default argument value of PHP map
- allows type safety fo array keyword while letting developers opt-into another layer:
ex. using string with specifying string details
function hello(string $arg) {
if (str_starts_with($arg, “wor”)) {
} else {
trigger_error
}
}
Cheers,
Josh