Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102828 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 85513 invoked from network); 14 Jul 2018 22:07:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jul 2018 22:07:41 -0000 Authentication-Results: pb1.pair.com header.from=marcospassos.com@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=marcospassos.com@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.46 as permitted sender) X-PHP-List-Original-Sender: marcospassos.com@gmail.com X-Host-Fingerprint: 209.85.218.46 mail-oi0-f46.google.com Received: from [209.85.218.46] ([209.85.218.46:34807] helo=mail-oi0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 38/50-39793-9247A4B5 for ; Sat, 14 Jul 2018 18:07:39 -0400 Received: by mail-oi0-f46.google.com with SMTP id 13-v6so68417994ois.1 for ; Sat, 14 Jul 2018 15:07:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=V40J2qSEQUIisSNhjrw+yYfu74wzzRQ6VkkClBmvPTs=; b=daL3Vawv8uwuJKQG6K+8q8XzE2dxlkR1nOvCG8wh1X9rNEPHSdSJ3glRaN20qCWq2x EhqM1xDwt0Zikn/eaDMiH84YBi1MLn4cGJIGBCOHA9yw+JgqRFunT22ZQsux5TU5/YlW 9SisjQMdoFJzHC0Py59BVDJyTSxA3iIJZovAkoetpmmuZVXn4UbDBmL/D1rdNiAOAdTe 4rlwZuSOPbq0YjvDRMhbwDUfHj64XlAPvxe4bQSHYVv5myzkzLQWGYiwM0AW0zstRS0D lWl80rdFBnGpZ99p1z/ZWjbbYKmfnb64mvwXK+cnudRYR+sNAGsVmIsSz8IiOBXOBFcW sZWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=V40J2qSEQUIisSNhjrw+yYfu74wzzRQ6VkkClBmvPTs=; b=Gt22/2L49AG8TFzj49VEqNvs4U48yOoQQes7RHfRlJRsQMlaoPyc4/rMKdMoxaOlB6 etT9cxOh+TFGkLTkjvobz9TDCU+OZXlYu6WDvZ34UE0Svx5bOZ4ey5CmiASqMcOrWA60 5Tcds9xSVVUpsIYcxNMrEnkRhU7RhwkpF24opDyC/81/g0hI0EtiuiS0Oihtyl1iXgHy i69Z10V2s+t+hksRCpaQ60r2n/Vm11RFbm/SlqyVjz69LHf140jgPtXDNYRbsk2U6Fyg F05uTFDcWXwL8LmG4SMApitlrL9U2QqwrJ9gsBnsRGC2fNNwTBEDWbhNxLkb6Wq5mcXX zNig== X-Gm-Message-State: AOUpUlGR5bKOegqRvTpjL1ckv3OZMCQ+bVVOjw+lFROIHiX9HqFh7xCn //+n8V0lzTv7Cu151sEY0xWt8GzHTDdph4TzB1g= X-Google-Smtp-Source: AAOMgpfMYogzdTX6XiNLPZWbqNkuhp/wRroxtICIehnkI2cd9yY0Jyomi5q6U90e+5hLbzJRbSPo9TA0gK7kv5etaJM= X-Received: by 2002:aca:2c02:: with SMTP id s2-v6mr13291157ois.82.1531606055411; Sat, 14 Jul 2018 15:07:35 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a8a:10b:0:0:0:0:0 with HTTP; Sat, 14 Jul 2018 15:07:14 -0700 (PDT) In-Reply-To: References: Date: Sat, 14 Jul 2018 19:07:14 -0300 Message-ID: To: Levi Morrison Cc: enno.woortmann@web.de, internals Content-Type: multipart/alternative; boundary="000000000000de64240570fcd2e4" Subject: Re: [PHP-DEV] [VOTE] array_key_first(), array_key_last(), array_value_first(), array_value_last() From: marcospassos.com@gmail.com (Marcos Passos) --000000000000de64240570fcd2e4 Content-Type: text/plain; charset="UTF-8" A function like array_offset would be convenient as there is no way to access the value of an array entry by its index currently. The workaround is using array_slice, with all the downsides already pointed out. 2018-07-13 20:04 GMT-03:00 Levi Morrison : > On Fri, Jul 13, 2018 at 4:50 PM Levi Morrison wrote: > > > > On Fri, Jul 13, 2018 at 4:48 PM Levi Morrison wrote: > > > > > > On Mon, Jul 9, 2018 at 5:17 AM Woortmann, Enno > 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`. > > Unfortunately I was too hasty: `array_slice` will iterate over the > entire input range: > > https://github.com/php/php-src/blob/b9963969fd088dca6852483fdb1c6b > 7e1080764d/ext/standard/array.c#L3538-L3552 > > We could add an case for when `array_slice` uses `-1`. I wonder how > common this already is. Does anyone have a tool that easily can search > open source code for usages of functions? I am interested in this > regardless of the outcome of this RFC. The documentation for > `array_slice` should also be updated independently of this RFC as > well. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --000000000000de64240570fcd2e4--