Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127074 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 9A0711A00BC for ; Tue, 8 Apr 2025 18:24:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1744136532; bh=V1Dc2zVdXcSyyDSQ3VJfgBALu5mduGup20KzLhwz1bk=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=nzS7GaUGE2OQ6rA74IMTRxz5lsqXduN4Q2VYHL02z3mrbgnM9AzbIBzU84mxTl/LO JXi4WO1HbCzjDP5oAG7Bh6A+x9rT8rotOuwP7c+tqPs+wQt0/pvj2r3SsaLC1HZ1w9 xb4CyGazU+iTzLu5AkDzDFVHktd2/hcHqlSJGxkYxQyybbyvVeRgF4ChRGRgJ7dfA8 CkOMlqmX5tVODTl0LI5V1eq3nHRpxZ/6HzimKupHc5dzbVD7eqBxeMU3sGnBzuvLBP yMTczwMSR/8iDQk327AC1ABvH7uMKgtM5ISSffBD7lhnQtgU/oX/v5qfRLPsoswnJ7 LIRvgaTFMb/SQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1D23618007D for ; Tue, 8 Apr 2025 18:22:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 8 Apr 2025 18:22:11 +0000 (UTC) Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-6fee50bfea5so49005427b3.1 for ; Tue, 08 Apr 2025 11:24:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech.net; s=google; t=1744136675; x=1744741475; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=IJHmo/qEwSfNmdHDiaXJLyiYp2WvTuTJ/G+XO8gTq8k=; b=eFnJc+aUgWIN66rnomd454RRecwsxrWSMi3hFH0y4ieVCLKzvLqrMxKv43atQPARHK cNJa7HtA7uyjIBNPAmBY1E1a099vvcURebNyEVuTrJcdTHw76gClFlvCUgvxjyijgBkr lLTGOw9loXdSzLML4vk4XaCwlSz2R+BowFXle1znS8k2+jNGgW2a8Chfw3HBxPglaHh3 3zuS3AIvGOcwjilW5+d6X+3Qg3AxUkGLdsYlvMeM6vZcNes+bT93kG7etpA5Nz4jbZH3 XWmgYmNi3Qa+/zRJRXJukFpNA++MWCU9xxXGYwwp6k/oSreSpyqgqXj+eHDXCDrlAdSY JS8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744136675; x=1744741475; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IJHmo/qEwSfNmdHDiaXJLyiYp2WvTuTJ/G+XO8gTq8k=; b=lMpAqKZ2yLvIitJZrgxdZGEL5XTMAzrRNOTtuZi/I3JZ9H2PTzHv4DcWFaPVwV6eYs CFyVNsTvN/9loQ4G9faI0Dn8byqtcPepQab2aJwbAeNo58OAkgfBEcK2O8Ra+5My+Lnb 5YEIEHMjJKHQsyicz66L+f7MrsacqzcfgqUI2gVMHlTQaaayLA581HQNORn8rPo9xdY5 p569xNTQhzeuYp5v0Ac0PZdfc1cO23bS/GkZKs1isqr4Vg93EJ7Ib4QQmE2LXGnsuaYq nA1l854NWvWq6hAtZd5NE+t8SaKfvpjswWNzjGHyJQzs9s6Dl8mgaPdEYH7ABm+MlExH fB6w== X-Forwarded-Encrypted: i=1; AJvYcCVWl532HxEc9bIRpzo8tKMQBf/mwOJPjX5CFIwowkJ+TWjl/DgYB5u2RR9dnUb/nSxcab2XScDMK4Q=@lists.php.net X-Gm-Message-State: AOJu0YzZFS1inxMtTbebK/xpkP70YW5raYRDrIOaCdWINKhv+9w9QvWY 1geS4LPtLtCiV5yWeV3UNvdc9Zt4HlVhinP7snaC7EBHr2LC+LFV4yWCNtr1tnVcV/lpm/Nz3kE z6yA3tHMNF1+oRux6ZwmC0V/4rSrW6HumW34++3zMVSQvPRNWfxQ= X-Gm-Gg: ASbGnctSp6+MimY4Vx3kYf5yJz8Sglv/ZN0wvK9t73tZohOfvCIJ/4cN3aJz8/oAUmP uDGPBUZa7uJDjwoFY3U2kt1EaXk483uh3CoQdOHwtG7C/o1M32VHgZ8+ebVZWbF6LwRNp+J5R/8 YBLpUu4asMeZ4iHk/xZYBqGQri X-Google-Smtp-Source: AGHT+IF+IFbmesksKr04CDTwbUOE81dZE0M7c12O1sLTbwMSogCSUHQAw7fzzYh7wbG5/WfHrZqt026Sh2bj8IIlZLk= X-Received: by 2002:a05:690c:4483:b0:6f9:7ce7:b107 with SMTP id 00721157ae682-705388e4053mr3845787b3.29.1744136675365; Tue, 08 Apr 2025 11:24:35 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <51fcec92-b2ae-482e-920f-c6c991b526c5@gmail.com> <9da1bf0b-482a-441d-9f16-6e94ffc9c871@scriptfusion.com> In-Reply-To: Date: Tue, 8 Apr 2025 20:24:24 +0200 X-Gm-Features: ATxdqUEY5SrsYXwgrdX4en2cRwCbT6FF4Fz0OUs22m7IcxUD-j8n6mB1kXuApwU Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] array_first() and array_last() To: Ayesh Karunaratne Cc: Bilge , internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: andreas@dqxtech.net (Andreas Hennings) On Mon, 7 Apr 2025 at 02:48, Ayesh Karunaratne wrote: > > > On Mon, Apr 7, 2025 at 2:05=E2=80=AFAM Bilge w= rote: > > ... [snip] I suggest first proving there is a > > legitimate need. > > I did a quick GitHub search for a common pattern of accessing an array > value by using the `array_key_first` and `array_key_last` functions: > > ```php > $value =3D $arr[array_key_first($results)]; > ``` > > - `[array_key_first(`: over 3,700 results[^1] > - `[array_key_last(`: over 4,300 results[^2] > > All of these hits can benefit from the proposed `array_first` and `array_= last`. (I used the wrong reply button earlier..) I suspect this is just the tip of the iceberg. You should look for reset() and end(). I get 336K when I look for "/(=3D |return |\(|\[)(reset|end)\(\$/ language:= PHP". I get 8.3K when I look for "/\[array_key_(first|last)\(/ language:PHP". There are more matches for "/reset\(" and "/end\(", but we only want matches where the return value is used. And indeed I want to never have to use reset() and end() again! I don't recall when I ever intentionally used the internal array pointer. Only as a workaround to get a first or last element. -- Andreas