Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121324 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 63347 invoked from network); 15 Oct 2023 12:40:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Oct 2023 12:40:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5E7211804AC for ; Sun, 15 Oct 2023 05:40:17 -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=-1.6 required=5.0 tests=BAYES_00,KHOP_HELO_FCRDNS, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS16276 192.99.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mail.processus.org (ns563681.ip-192-99-44.net [192.99.44.131]) (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 ; Sun, 15 Oct 2023 05:40:16 -0700 (PDT) Message-ID: <72198e55-b2b2-485c-b602-c68b76694368@processus.org> Authentication-Results: mail.processus.org; auth=pass smtp.mailfrom=pierre-php@processus.org Date: Sun, 15 Oct 2023 14:40:11 +0200 MIME-Version: 1.0 Content-Language: en-US To: Ben Ramsey , Nikita Popov Cc: David Grudl , Levi Morrison References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spamd-Bar: + Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: pierre-php@processus.org (Pierre) Le 15/10/2023 à 01:11, Ben Ramsey a écrit : >> On Oct 14, 2023, at 16:30, Nikita Popov wrote: >> >> On Sat, Oct 14, 2023, at 20:00, David Grudl wrote: >>> PHP lacks two very basic functions for working with arrays: >>> >>> - array_first() returning the first element of an array (or null) >>> - array_last() returning the last element of the array (or null) >>> >>> 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. >>> >>> 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". >>> >>> Much worse, they shouldn't to be used to get first/last value, because >>> they have a side effect (i.e. moving the pointer). >>> >>> 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(). >>> >>> b) What about $array[array_key_first($array)]? >>> >>> 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. >>> >>> c) Two such functions were proposed and rejected during the >>> array_key_first/last RFC >>> (https://wiki.php.net/rfc/array_key_first_last) >>> >>> 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. >>> >>> DG >> I'm in favor of adding these. >> >> 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 to a modification you are not actually interested in. >> >> 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($array)], which is rather convoluted, and incorrect if the array is potentially empty. >> >> Regards, >> Nikita > I’m in favor of these functions, for all the same aforementioned reasons. Yes please ! array_first() and array_last() are definitely needed. I wrote `foreach ($foo as $value) break;` too many times in my life. array_key_first() and array_key_last() I wouldn't use it much, but they'd probably  find their use cases as well. The first two probably only make sense for a numerically indexed array, so I guess that array_is_list() (whatever the name is, I don't want to bikeshed about naming) would be a good addition as well, that, in my opinion, would be pertinent to add at the same time. Regards, Pierre