Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121314 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13470 invoked from network); 14 Oct 2023 20:30:26 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Oct 2023 20:30:26 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id ACC221804B4 for ; Sat, 14 Oct 2023 13:30:24 -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,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-qt1-f177.google.com (mail-qt1-f177.google.com [209.85.160.177]) (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 13:30:21 -0700 (PDT) Received: by mail-qt1-f177.google.com with SMTP id d75a77b69052e-419bd101c61so7369661cf.1 for ; Sat, 14 Oct 2023 13:30:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697315420; x=1697920220; darn=lists.php.net; h=subject:to:from:date:references:in-reply-to:message-id:mime-version :user-agent:feedback-id:from:to:cc:subject:date:message-id:reply-to; bh=D1awAmhVl7TjeWRJLBhLs5VEDV602GCA4Co6QQ1O/EI=; b=OsO3dSv/8NKji7Pnb23oSj0OyTyuwKyIYwTBhFn/MqfJy+AaZOSS+qVu0QYcaUlyae R1WEKA18NmSSHspz7n/rFcsdvyQnuZSAEMhbuD4+mEcIUJch0noQXiV9+UFYRC+1DGBY wtbWi2GygAfuZVwSrKnNV/ctvK7jpBj7WIl/f+bKdVDCEmJF2ipqUXtYYwXbrHZw8+Mk SdxXWQE3463tHZ6KVCz7w2kzH4iIzRqeWfCNqFwrJpw8PC0T1cNJ7xOLDeeSy2KhDNn5 /hnHlUxRAm0zfoNUeYoye5ZfZrvYPXCfnjyIxkwzCC+TQ/I9k2Ld6WF4irm8G+FPDgg6 teoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697315420; x=1697920220; h=subject:to:from:date:references:in-reply-to:message-id:mime-version :user-agent:feedback-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=D1awAmhVl7TjeWRJLBhLs5VEDV602GCA4Co6QQ1O/EI=; b=aqgQQ597LdJKFHkpq7pW3VnvPBvw6t1v7eMdj/pOzd+yBPQ2hCdq4XkuO2Z1PSjrJN EKnkLNSLySA8Js4tytx4v3xleEmwOiKIFAC61Nk28KofnHwbX51lhdamvzTUv42VwXMr Xjbsk/sGHE74ElxFnvJzM/yqwYH9Iyq3XuGm29ChuTiJd2VN8E83CdkWvCm8FXpW4OwD 7WT0ijWj2/0RD9O7CHCOngpBMSdpAbEOYJXo4CqNKB0zICOQs/zmoVVWzAYo/JPxnL6r 7Ga+FUvaZqK5wscssRmqYkc0nDVV63bm0SMROm8ZYQWfwWovhg0F4VJAjMwJGyElRDhw +v+g== X-Gm-Message-State: AOJu0YwX6PxRWgUIBIiyVR5k4eup18t3dXaKYapkg9cJ3G+6TLG7jPCt b6QtM/Z/4Lo12kPJpFJvcJ0HpvzErIA= X-Google-Smtp-Source: AGHT+IFa1cHk3/0W9s021ORvqTHQ9ktqgyzQZTeWjDqezX+OJIT/09UzA+gmlFF/vB+9V753Wrlv/Q== X-Received: by 2002:a05:6214:2303:b0:658:30c4:206 with SMTP id gc3-20020a056214230300b0065830c40206mr33294704qvb.0.1697315420296; Sat, 14 Oct 2023 13:30:20 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id dm9-20020ad44e29000000b0066d15724ff7sm1951701qvb.52.2023.10.14.13.30.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Oct 2023 13:30:19 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailauth.nyi.internal (Postfix) with ESMTP id 331D227C005C; Sat, 14 Oct 2023 16:30:19 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute4.internal (MEProxy); Sat, 14 Oct 2023 16:30:19 -0400 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrieehgddugeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesrgdtreerreertdenucfhrhhomhepfdfpihhk ihhtrgcurfhophhovhdfuceonhhikhhithgrrdhpphhvsehgmhgrihhlrdgtohhmqeenuc ggtffrrghtthgvrhhnpeegleduuddukeeftefgkeefgeetffehudetgfettdekheegieeh tdffgfelhfffvdenucffohhmrghinhepphhhphdrnhgvthenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehmrghilhdomhgvshhmthhprghuthhh phgvrhhsohhnrghlihhthidqudefudefledukeehiedqvdehkedvgeehieekqdhnihhkih htrgdrphhpvheppehgmhgrihhlrdgtohhmsehnphhophhovhdrtghomh X-ME-Proxy: Feedback-ID: id4a9467a:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 9A07331A0065; Sat, 14 Oct 2023 16:30:18 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-1019-ged83ad8595-fm-20231002.001-ged83ad85 MIME-Version: 1.0 Message-ID: In-Reply-To: References: Date: Sat, 14 Oct 2023 22:29:28 +0200 To: "David Grudl" , "Levi Morrison" Content-Type: multipart/alternative; boundary=fbf4929af1964f3c8d23fc32579ad4ae Subject: Re: [PHP-DEV] Two new functions array_first() and array_last() From: nikita.ppv@gmail.com ("Nikita Popov") --fbf4929af1964f3c8d23fc32579ad4ae Content-Type: text/plain 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 --fbf4929af1964f3c8d23fc32579ad4ae--