Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102815 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20322 invoked from network); 13 Jul 2018 22:48:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2018 22:48:47 -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.43 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.215.43 mail-lf0-f43.google.com Received: from [209.85.215.43] ([209.85.215.43:37966] helo=mail-lf0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9B/39-39793-D4C294B5 for ; Fri, 13 Jul 2018 18:48:45 -0400 Received: by mail-lf0-f43.google.com with SMTP id a4-v6so28374689lff.5 for ; Fri, 13 Jul 2018 15:48:45 -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=2lPzAWp7e2EKc6DmEbfnZ4w622AW2tEejD8w77OEs44=; b=iB7ulu+hs7Rzt889owKSnbVcj6HV3CZZQ2Hrlhht5m4FMK4Jngjm/Dq6Z9lVMupjlb bwJZP4GMIFkCvLbbqxbufRn/1CBsEyE/xwyqIr/L4dDID3QEjCBU6YqnN7ciKtgkmhxn OX2XyFKjIHXSGTuVIBQMKAtTq59N4df7IjR8e3ArouaDZq9cn7H8E6w+0k3R8F+yGyrT 935FyHlKs4yPJOeNTkFn+5gGJqkenZ1DVoMfqVTLllvtSFqHudg4AItZ6GQSfYW9udTL N5BdjtdwrEZ65b/nnk8oa+jBBoh7RfsdnScLpezC+jsbmrO84hrtaiVNSAnEO+cjzQ1P 2m+g== X-Gm-Message-State: AOUpUlExFUO39rQIwediR/nVerLm4lqMO05Ztxnl1MQysihFzBGOsnel YnL3KR3qCQcbdpURq0qSvaMKwdawW0zEzN3tDDE= X-Google-Smtp-Source: AAOMgpdgJA2OLB3ycCDcHGAQPF4jZvv0sy5laDcpt0d4/tm3IRgK7SIwJKHyXsKEtgBA0Jku8t7nlakulNytfh4Rkks= X-Received: by 2002:a19:5d54:: with SMTP id p20-v6mr5981727lfj.143.1531522121849; Fri, 13 Jul 2018 15:48:41 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 13 Jul 2018 16:48:29 -0600 Message-ID: To: enno.woortmann@web.de Cc: internals 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 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