Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102835 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62266 invoked from network); 16 Jul 2018 08:26:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jul 2018 08:26:40 -0000 Authentication-Results: pb1.pair.com smtp.mail=come@opensides.be; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=come@opensides.be; sender-id=pass Received-SPF: pass (pb1.pair.com: domain opensides.be designates 195.154.20.141 as permitted sender) X-PHP-List-Original-Sender: come@opensides.be X-Host-Fingerprint: 195.154.20.141 smtp.opensides.be Received: from [195.154.20.141] ([195.154.20.141:39956] helo=smtp.opensides.be) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4C/36-39793-EB65C4B5 for ; Mon, 16 Jul 2018 04:26:40 -0400 Received: from localhost (localhost [127.0.0.1]) by smtp.opensides.be (Postfix) with ESMTP id B7C03BBF7C for ; Mon, 16 Jul 2018 10:26:35 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at opensides.be Received: from smtp.opensides.be ([127.0.0.1]) by localhost (smtp.opensides.be [127.0.0.1]) (amavisd-new, port 10024) with LMTP id HJh9U-LWVB73 for ; Mon, 16 Jul 2018 10:26:32 +0200 (CEST) Received: from mcmic-probook.localnet (63.120.199.77.rev.sfr.net [77.199.120.63]) by smtp.opensides.be (Postfix) with ESMTPSA id 933B4BBFB4 for ; Mon, 16 Jul 2018 10:26:31 +0200 (CEST) To: internals@lists.php.net Date: Mon, 16 Jul 2018 10:26:14 +0200 Message-ID: <2611519.nOfBZULHxk@mcmic-probook> Organization: OpenSides User-Agent: KMail/5.2.3 (Linux/4.9.0-6-amd64; KDE/5.28.0; x86_64; ; ) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" Subject: Re: [PHP-DEV] [VOTE] array_key_first(), array_key_last(), array_value_first(), array_value_last() From: come@opensides.be (=?ISO-8859-1?Q?C=F4me?= Chilliet) Le vendredi 13 juillet 2018, 16:48:29 CEST Levi Morrison a =E9crit : > 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`: >=20 > function array_offset(array $input, int $offset): ?array { > $slice =3D array_slice($input, $offset, 1, true); > return count($slice) ? $slice : null; > } >=20 > $assoc =3D ['one' =3D> 1, 'two' =3D> 2, 'three' =3D> 3]; > $packd =3D range(1, 4); >=20 > var_dump(array_offset($assoc, -1)); > var_dump(array_offset($packd, -1)); >=20 > var_dump(array_offset($assoc, 0)); > var_dump(array_offset($packd, 0)); >=20 > Of course, the `array_slice` function can be used to build all of the > functions described in the RFC, as well. How would you do that, since array_slice still gives you an associative arr= ay as a result? array_values(array_slice($array, -1, 1, true))[0] is way less readable than= array_last_value($array), and the question =ABhow efficient is that and ho= w many array copies are made?=BB becomes complicated to answer for non-init= iated. While with a native function you expect it to be as efficient as possible t= o do what you ask it. (And as you state in your other mail in the end this array_slice solution i= s not as efficient as you expected) I would have been fine with array_key($array, $offset=3D0) and array_value(= $array, $offset=3D0) instead of the 4 functions from the RFC to be able to = use any offset. C=F4me