Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:102816
Return-Path: <morrison.levi@gmail.com>
Mailing-List: contact internals-help@lists.php.net; run by ezmlm
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 21651 invoked from network); 13 Jul 2018 22:50:52 -0000
Received: from unknown (HELO lists.php.net) (127.0.0.1)
  by localhost with SMTP; 13 Jul 2018 22:50:52 -0000
Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass
Authentication-Results: pb1.pair.com header.from=levim@php.net; sender-id=unknown
Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.47 as permitted sender)
X-PHP-List-Original-Sender: morrison.levi@gmail.com
X-Host-Fingerprint: 209.85.215.47 mail-lf0-f47.google.com  
Received: from [209.85.215.47] ([209.85.215.47:36219] helo=mail-lf0-f47.google.com)
	by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP
	id F9/89-39793-ACC294B5 for <internals@lists.php.net>; Fri, 13 Jul 2018 18:50:51 -0400
Received: by mail-lf0-f47.google.com with SMTP id b22-v6so15960219lfa.3
        for <internals@lists.php.net>; Fri, 13 Jul 2018 15:50:50 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:references:in-reply-to:from:date
         :message-id:subject:to:cc;
        bh=7u38cfv8/UvPpxcKrqUtLXSKBlh6BKzQdb248T8P8QQ=;
        b=OiXLxE9K+Rsk+f0W/tFtOiiMf0P1GUt61jSQk8jiqOSNYf5Wte8XKV+5SoQndQJ2Gl
         xuuzXI7J4r3AW7SZzKyCOHTCnBchgTtDI6Nm/cGbzJwf9PjBT6OJeSGdsV0MR6vqPidt
         51f1DTq3mlA1prvq+qrcppl0hFcD0ykGYkdY5IbZLOt/+J2d1TWKut4XMTlXP+7l3cwq
         XSWxZt/GAKINqOcvQzMQfpAFLckN8/5B0o6JPNaSN6+TH0Hz4L65bQzppjM1cNP+rp2Z
         +kHyCsZAL83vKbuehGnuufi2Xx5pyItYqO03SfaMagSDr//KM5lMmUuPFB3ny9wCR4Zl
         Nzag==
X-Gm-Message-State: AOUpUlFuONJl1BRYcfYvRBjJ8arIYjwqp8FEoLN8p1mnEtokhCB4i3td
	ONMZmfr1a3J5zAT09HPC+v2G3KQ0x0v+Z22bjxU=
X-Google-Smtp-Source: AAOMgpf7/cNnLZxuxZaRBelhTO+M0R4VbRDuawNuXAB/nSxQgS/Sn7rRuMgKWSL7enNlXRSy6ZZ1++kafeZq1MPcyM0=
X-Received: by 2002:a19:1749:: with SMTP id n70-v6mr5996486lfi.54.1531522247715;
 Fri, 13 Jul 2018 15:50:47 -0700 (PDT)
MIME-Version: 1.0
References: <e8fdd7ca-135a-2a7f-79cd-e45486f4ba8b@web.de> <CAFMT4Nqyc=iJHWTPtQs_Sw=PfxsAoNkXc1Q4JKcedw2sziLbJg@mail.gmail.com>
In-Reply-To: <CAFMT4Nqyc=iJHWTPtQs_Sw=PfxsAoNkXc1Q4JKcedw2sziLbJg@mail.gmail.com>
Date: Fri, 13 Jul 2018 16:50:35 -0600
Message-ID: <CAFMT4NrMKq3aWUyk_mf-nLKmo6Nqes7OSNBmwTz8Npcsg-k=-w@mail.gmail.com>
To: enno.woortmann@web.de
Cc: internals <internals@lists.php.net>
Content-Type: text/plain; charset="UTF-8"
Subject: Re: [PHP-DEV] [VOTE] array_key_first(), array_key_last(),
 array_value_first(), array_value_last()
From: levim@php.net (Levi Morrison)

On Fri, Jul 13, 2018 at 4:48 PM Levi Morrison <levim@php.net> wrote:
>
> On Mon, Jul 9, 2018 at 5:17 AM Woortmann, Enno <enno.woortmann@web.de> wrote:
> >
> > Hi,
> >
> > as the discussion got no new contributions I'd like to start the voting
> > for the RFC fo add new functions for the handling of outer array elements.
> >
> > https://wiki.php.net/rfc/array_key_first_last
> >
> > To have a better separation I split up the vote for the functions. The
> > first vote covers the functions to handle keys: array_key_first() and
> > array_key_last(). The second vote covers the corresponding functions to
> > handle the values: array_value_first() and array_value_last().
> >
> > As this RFC adds functions but doesn't change the language syntax a 50%
> > + 1 majority is required for both votes. The votes are open until
> > 2018-07-16.
> >
> > The discussion for this RFC is located at
> >
> > https://externals.io/message/102245
> >
> > Regards,
> >
> > Enno
> >
> >
> >
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
>
> This entire time I felt like this should be possible in pure PHP.
> Surely, somewhere, we have this capability already? I searched quite a
> few functions but didn't find anything.
>
> However, the feeling was right. Just moments ago Paul Crovella from
> Stack Overflow mentioned to me that `array_slice` uses the terminology
> `offset` to refer to the order of the entry rather than its key, so I
> went looking. As far as I can tell from the [array_slice
> implementation][1] it will not trigger copies and will be efficient if
> using -1 to retrieve the last index. We can provide the parameter
> `preserve_keys` an argument of true to get both the key and the value.
> These functions *are* efficiently implementable in user-land!
>
> Below is a proof-of-concept for the `array_offset` function [mentioned
> by Nicolas Grekas][2] (which by the way, neither the RFC author nor
> anyone else responded to this suggestion) that is simply a convenience
> wrapper over `array_slice`:
>
>     function array_offset(array $input, int $offset): ?array {
>         $slice = array_slice($input, $offset, 1, true);
>         return count($slice) ? $slice : null;
>     }
>
>     $assoc = ['one' => 1, 'two' => 2, 'three' => 3];
>     $packd = range(1, 4);
>
>     var_dump(array_offset($assoc, -1));
>     var_dump(array_offset($packd, -1));
>
>     var_dump(array_offset($assoc, 0));
>     var_dump(array_offset($packd, 0));
>
> Of course, the `array_slice` function can be used to build all of the
> functions described in the RFC, as well.
>
> My new opinion is that no new functions are required and that
> improving the `array_slice` documentation is all that is necessary. It
> currently does not show any examples of it working on associated
> arrays, which is probably why none of us (many of us experts) realized
> this through this discussion. This is especially true as "offset" in
> some other situations really means "key", as in
> `ArrayAccess::offsetGet`.
>
>   [1]: https://github.com/php/php-src/blob/b9963969fd088dca6852483fdb1c6b7e1080764d/ext/standard/array.c#L3477-L3577
>   [2]: https://externals.io/message/102245#102322

Small correction: Nicolas used the name `array_index`, not `array_offset`.