Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121319 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 34916 invoked from network); 15 Oct 2023 03:20:30 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Oct 2023 03:20:30 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 55370180504 for ; Sat, 14 Oct 2023 20:20:28 -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, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS200350 178.154.224.0/19 X-Spam-Virus: No X-Envelope-From: Received: from forward502b.mail.yandex.net (forward502b.mail.yandex.net [178.154.239.146]) (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 20:20:27 -0700 (PDT) Received: from mail-nwsmtp-smtp-production-main-54.iva.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-54.iva.yp-c.yandex.net [IPv6:2a02:6b8:c0c:1e27:0:640:d1a0:0]) by forward502b.mail.yandex.net (Yandex) with ESMTP id D87635EAF4 for ; Sun, 15 Oct 2023 06:20:24 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-54.iva.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id NKYQLtPDda60-3WEybpO6; Sun, 15 Oct 2023 06:20:24 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=php.watch; s=mail; t=1697340024; bh=zH6CZLJuGxQyTTG/1UqTOjf8mkFCBqnetla+4/sQsM4=; h=To:Subject:Message-ID:References:Date:From:In-Reply-To:Cc; b=O0sNPfcC6W2+zhXLAXbxcaMhpT031F41cejurwEsA2Prcncc/JRqYqNHQ7jGmSLYb mW0aqA4S8c1UUueBWwkS509Hui0awCCe3qUtbGZPVPFbncFl+7Fsg56vOZkMoFUz6W iTKCoCaJIGcH/lDqoP1O3y5AFAduQOvzRAVEzyA8= Authentication-Results: mail-nwsmtp-smtp-production-main-54.iva.yp-c.yandex.net; dkim=pass header.i=@php.watch Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-50797cf5b69so2672199e87.2 for ; Sat, 14 Oct 2023 20:20:24 -0700 (PDT) X-Gm-Message-State: AOJu0Yz+eDcYAfVJrCXFieO+Vt4gxT8XuSgnb69m2zUx3LazU+Q4R9Dd NQAFOjr0lpoeR3bTusqsW+888TcQyE7fjr8Bw2E= X-Google-Smtp-Source: AGHT+IGXhGDSL83mA/9Ydyck6ebxKNfyuyDaSCpA15lFl8LKEfhaxsKQCN2S36iG4ICLCsrw85qOWsH3G3iZ13p7aZ8= X-Received: by 2002:a05:6512:102a:b0:503:9c2:e44e with SMTP id r10-20020a056512102a00b0050309c2e44emr21011770lfr.55.1697340023772; Sat, 14 Oct 2023 20:20:23 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 15 Oct 2023 10:19:57 +0700 X-Gmail-Original-Message-ID: Message-ID: To: David Grudl Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: ayesh@php.watch (Ayesh Karunaratne) > > 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 would love to have these functions in PHP as well. With the bikeshed risk, perhaps it makes more sense to have the functions named `array_value_first` and `array_value_last`, because we already have `array_key_first` and `array_key_last` functions?