Re: Add is_vectorlike($array) function
| From: | Rowan Collins | Date: | Sat, 06 May 2017 15:42:49 +0000 |
| Subject: | Re: Add is_vectorlike($array) function | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-98979@lists.php.net to get a copy of this message | ||
On 5 May 2017 22:19:51 BST, Ryan Pallas <derokorian@gmail.com> wrote:
>I just read this thread and am wondering what exactly is the use case?
>Like
>are you going to do something if it is vector-like, and do something
>different (or not do anything at all) if it's not vector-like? I mean,
>if
>you have to crawl it, and need a vector, why not just call array_values
>and
>guarantee you have a vector?
I gave one use case in an earlier message: many serialisation formats have a different form for
ordered lists vs key-value pairs. As an obvious example, look at JSON arrays and objects, and many
other formats have similar types.
It can also be quite intuitive to have a function accept either a list of prepared items or a set of
key-value pairs representing structured data. For instance, HTTP headers might be prepared like
['User-Agent: example'] or structured like ['User-Agent' =>
'example']. Turning the second into the first requires more than just running
array_values, but can be skipped if the input is known to be vector-like.
Arrays that have integer keys, but not all sequential, can be a bit of an edge case, but treating
them as a list throws away information, so it's usually safer to treat them as key-value pairs.
Regards,
--
Rowan Collins
[IMSoP]