Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121316 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21740 invoked from network); 14 Oct 2023 23:12:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Oct 2023 23:12:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 69162180504 for ; Sat, 14 Oct 2023 16:12:18 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 14 Oct 2023 16:12:17 -0700 (PDT) Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-5a7b92cd0ccso41353607b3.1 for ; Sat, 14 Oct 2023 16:12:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=benramsey.com; s=google; t=1697325136; x=1697929936; darn=lists.php.net; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date:message-id :reply-to; bh=NRFBp2DQEUccAaw4n9s3VtFlzVdzbwFaOqHOiKEnT4w=; b=BErv/LjzSc2o0A/SMc+i5yb1Wpo017Hac8rE7z89rRdaqSZQ72VlZmNFh0LDFsjrPp E496g8XQ6l7mhkB6mBAASd4HQP2OrCk22uVFa/stdDXUJ3z2joHJ2y2S3cdOpGi8vEnE LrYs0gHP3p3fXCRi5rF5Ef4Fd9KUmiBQIGw1j1ioh5A9YJQmzetTNl3wiiOvHtPAEODf 4qUORTHVIjIZsvJuqm039d2MW+GFSYpgh5VaxGlh1CMQUYhGAQPjMh6uOj35RvuNwy9D b9waFm1+VnPip/gIUsNLK8OSV6Esr3HD70g10Mz/3aBtodBNgmOfnHLXsks84X7MVLJF 8grA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697325136; x=1697929936; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NRFBp2DQEUccAaw4n9s3VtFlzVdzbwFaOqHOiKEnT4w=; b=EYABP9gcIkLDQmC7+adqmGNMGQP8qvU/KB0wtElaqA9R+5CCmttkWOG/A9yM/8fYIw 7kbZUIq/f6MjANCzYTx073Fvj0/ivKdAntQzpjfJejN1j865idHZBYkAL1N+P/tXRwyY 4+OEQfFr1yZxV7IwlnS5i3cGlzVmXWtBPFrqsvMqOVQDyJKyOxv2+oDhbpsfieCF7Edu Yk384x4HgBPMa/80f7Bxwe3MBxQNrvXySWoJnW3m7wP7v63Fc9HQhzFM3X6MXLWYswtb MC5MfXvEK3YvDuoej5Gdr620HnYjG9uRAkPAoKeGc9L3gXpD9kCbUu6TjwyOMgPnqq1N k6bQ== X-Gm-Message-State: AOJu0YxieLjbl+D7/Suc6tvP60BBT7SpSHe0pMj948KFkBLUXoh5QD1C sugU4L0Ub6aWimayum5C6Hwj8Fkkn11tg/DwtyM= X-Google-Smtp-Source: AGHT+IGtJJsoeSu/Z6VMCB6n5yUpoziZtiNxzzpR4HTyLnqWp4tkE8+1tvN8ng0ouYx4ZQhRISdslA== X-Received: by 2002:a81:48cb:0:b0:5a8:1d18:a16 with SMTP id v194-20020a8148cb000000b005a81d180a16mr5403215ywa.36.1697325136514; Sat, 14 Oct 2023 16:12:16 -0700 (PDT) Received: from smtpclient.apple ([2600:1004:b164:a452:20e7:f51:3ceb:9854]) by smtp.gmail.com with ESMTPSA id m18-20020a819c12000000b005a7b8fddfedsm903610ywa.41.2023.10.14.16.12.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 14 Oct 2023 16:12:15 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (1.0) Date: Sat, 14 Oct 2023 19:11:05 -0400 Message-ID: References: Cc: David Grudl , Levi Morrison In-Reply-To: To: Nikita Popov X-Mailer: iPhone Mail (20G81) Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: ben@benramsey.com (Ben Ramsey) > On Oct 14, 2023, at 16:30, Nikita Popov wrote: >=20 > =EF=BB=BFOn Sat, Oct 14, 2023, at 20:00, David Grudl wrote: >> PHP lacks two very basic functions for working with arrays: >>=20 >> - array_first() returning the first element of an array (or null) >> - array_last() returning the last element of the array (or null) >>=20 >> While PHP has functions that return the first and last keys, >> array_key_first() and array_key_last(), it does not have more useful >> functions for values. >>=20 >> a) What about reset() and end()? >> Programmers "abuse" the reset() and end() functions for this purpose. >> The problem is that these functions are used to move the internal >> pointer in the array. Which is why they have a name that is >> inappropriate when used in the sense of "return me the first element". >>=20 >> Much worse, they shouldn't to be used to get first/last value, because >> they have a side effect (i.e. moving the pointer). >>=20 >> Further, in the absence of an element, they return the obsolete false >> and not the currently expected null, which can be combined with the ?? >> operator. In this they differ from the similar functions >> array_key_first() and array_key_last(). >>=20 >> b) What about $array[array_key_first($array)]? >>=20 >> For such basic functions as returning the first and last item in an >> array, there should be a function in the basic package, not a >> workaround. Moreover, this requires having the array in a local >> variable, since $this->getFoo()[array_key_first($this->getFoo())] >> would be very inefficient and possibly incorrect. >>=20 >> c) Two such functions were proposed and rejected during the >> array_key_first/last RFC >> (https://wiki.php.net/rfc/array_key_first_last) >>=20 >> Yes, that was in 2018. At that time, functions like str_contains() or >> str_starts_with() wouldn't have even come into existence, just because >> there was an obscure way to do it without them. I believe we've moved >> on since then. Today we know how useful it is to use simple, >> easy-to-understand methods, both for programmers who write and read >> the code. >>=20 >> DG >=20 > I'm in favor of adding these. >=20 > To add to what you already said, because reset/end modify the array, there= 's a good chance that calling these functions will copy the whole array due t= o a modification you are not actually interested in. >=20 > So basically you have the choice between calling end(), which is the wrong= thing to do semantically and may be slow, or using $array[array_key_last($a= rray)], which is rather convoluted, and incorrect if the array is potentiall= y empty. >=20 > Regards, > Nikita I=E2=80=99m in favor of these functions, for all the same aforementioned rea= sons. Cheers, Ben