Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120508 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 91935 invoked from network); 2 Jun 2023 01:24:59 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Jun 2023 01:24:59 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id F239B1804D7 for ; Thu, 1 Jun 2023 18:24:58 -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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,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-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 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 ; Thu, 1 Jun 2023 18:24:58 -0700 (PDT) Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-568f9caff33so15501937b3.2 for ; Thu, 01 Jun 2023 18:24:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685669098; x=1688261098; 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=+gj9eAgjywm/ovqvIjrsncgBcUKi05XJyeeHQA8HXPY=; b=H4u/mjnlbqhKGQEtJYDnfDFijzV9NiFY7X+d8goR1aEFTseZzY0upxMKbDhaAzWOh4 NAaS8+DAYG/UR5wTWoJabvz0GuQ1L+zXyPEs6/qULcsAQOlGflTi7m4aWUD1MbaJ+rw3 Iru8dAunhUl7og6k2KguVNT+/ihUZ8LYG7HNXOOGVu8Rwj+lmlk95vHUqgCL2pCbUiEd SbaQN9/jn2LAGPlJibAtzbZt5lXcJm0G2fxLJOVPCLIuD7u2lX596E4IN/VDHo9OjOsv irMPzCV/vgWizkePlcH0hql/YNu1fj5IFZLyXnDDgZ4cAeZYwskgJ+K7dyAOvfklOELz ihig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685669098; x=1688261098; 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=+gj9eAgjywm/ovqvIjrsncgBcUKi05XJyeeHQA8HXPY=; b=SAbtZIamaiUn4u3m8nUkT3baNxEj85mUxtIMOte6XHYcxLbq3VeaWF6+fSj6CqlynI 77vz8ZogRa0L5rEoWDcb+Srnam9Af738D20O06R8pTNhjwX6mCx1JWbiej5wHUXJc7zk UkrqtQwwkFTdu1Tl8fN73jy3etvF/N2mp6wmuEM2FPd0UI/nSBTMUSdB7T5JMfBMhcf0 XB5n+VQrVr1sjSCFrgzWuxCiKMMoictg3FpLoTo5QCpVcsCFaWJ81fCo608hRRPH0YMx VdNFy++uNlfUB9NWpf8klr3dc5b1NTJCDGs7HIMLbp3fpAFaZsWbReiZ/mfykU+HNLsd uErw== X-Gm-Message-State: AC+VfDzO5c5reg7KeOr6K/+EWf5kP+d6RD6P9Y7jENUF0VMTmV4Grrm/ uW27yTGuT+6VsFYu8TgBojqMe93z57xrrIKEs9w= X-Google-Smtp-Source: ACHHUZ5zGbWrGHHI8dzIr8iA5AugBogdLtX+Ox/O/eRkEsMPSiNtaROx77kE/5yIT8wYNRy4T5ZBzYO7Jwfjr5XOtzY= X-Received: by 2002:a0d:cb57:0:b0:55a:985e:8ac9 with SMTP id n84-20020a0dcb57000000b0055a985e8ac9mr10572350ywd.22.1685669097735; Thu, 01 Jun 2023 18:24:57 -0700 (PDT) MIME-Version: 1.0 References: <97308c1d-e9f0-e28d-78ba-11da2a136ee6@fide.pl> <6c66ad82-7616-4b22-8961-494b557a3def@app.fastmail.com> In-Reply-To: <6c66ad82-7616-4b22-8961-494b557a3def@app.fastmail.com> Date: Fri, 2 Jun 2023 03:24:20 +0200 Message-ID: To: Casper Langemeijer Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Proposal: addition of array_find() and array_find_key() functions From: divinity76@gmail.com (Hans Henrik Bergan) sounds like array_find could be implemented by just adding a new flag for array_filter's $mode: ARRAY_FILTER_STOP_ON_FIRST_MATCH or some such? On Fri, 2 Jun 2023 at 02:15, Casper Langemeijer wrote= : > > On Thu, Jun 1, 2023, at 18:02, Janusz Szczypka wrote: > > array_find(): This function would allow developers to find the first el= ement in > > an array that satisfies a specific condition. The condition would be de= fined by a callback function. > > This would actually be an alternative to a simple foreach() with an if() = and a break. As your example implementation makes clear. > > array_find() and array_find_key() would still do the same internally, but= add a function call adding the overhead of pushing parameters to stack, sw= itching the scope, passing return value back switch scope and pop from the = callstack again. If parameters or return value are typed this adds even mor= e overhead. > > I don't think there is any optimisation possible when this is done in C. > > To the proposer it probably feels that this is an array function currentl= y missing, but instead it's probably better not to have array_find() becaus= e people might actually use it for larger datasets. > > Although I acknowledge that it will reduce the number of lines of PHP cod= e it doesn't actually reduce the mental capacity to read and maintain code.= I find the simple elemental building blocks of loops, conditionals and exp= ressions easier and more expressive. In my experience this is especially so= for less experienced developers. > > Then there is a problem of naming. array_search() and array_find(). To a = new developer, how would they know which is which? > > Also there is the missing array_find_value_and_key() method. Because why = should we be limited to either matching the key OR the value? > > Greetings, Casper >