Re: Add is_vectorlike($array) function
| From: | Ryan Pallas | Date: | Sat, 06 May 2017 17:47:54 +0000 |
| Subject: | Re: Add is_vectorlike($array) function | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-98980@lists.php.net to get a copy of this message | ||
On Sat, May 6, 2017 at 9:42 AM, Rowan Collins <rowan.collins@gmail.com>
wrote:
> 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.
>
Ah, so you would do something like
private serializeArray(array $data) {
return is_vector($data) ? $this->serializeVector($data) :
$this->serializeAssoc($data);
}
is that right?
> 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.
>
Ah, that's a great example and makes perfect sense now! Thanks :)
>
> 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]
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>