Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120502 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 56937 invoked from network); 1 Jun 2023 16:30:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Jun 2023 16:30:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BF2F7180083 for ; Thu, 1 Jun 2023 09:30:09 -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.3 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout1-smtp.messagingengine.com (wout1-smtp.messagingengine.com [64.147.123.24]) (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 09:30:09 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id D61763200921 for ; Thu, 1 Jun 2023 12:30:07 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Thu, 01 Jun 2023 12:30:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm1; t=1685637007; x=1685723407; bh=u4mQPz+bwe E8lpgB7fpdQGuj68b1lQ0L8YHPdyN/cxc=; b=RAXFB0qTGPUWmD9s7XNglV+kfm 9OVei1MTuwqMQf/SrVCmHuP535xk/O/UTUzmBZy7e75bdtYAc9MrrOPZgP6UgDE+ ybnmwF99hk7K5E74hXsFbq5gPQ9bZ3rTyeTDOIysXg4iSAnLH4bcOJHq/y3TuWqL NkeLzP/uKbIuHKSUhfIB++zML+ZudekVXFgEzyYtK5qy1og+Z2lIQPDtQdfJ/lJB QOBJv/rdOg14jf7nR5Mb73YoR27XNCpRhyhrLIov1twORurWPt4ksdcn5Gg0HIXz 9GLFjNSsBTuzBC8Jcs34+INHNyb2RxnfxJZFPr56bAHPMtTL7d8oZJ7Aql+A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1685637007; x= 1685723407; bh=u4mQPz+bweE8lpgB7fpdQGuj68b1lQ0L8YHPdyN/cxc=; b=j PH5w+4hmrP4lliEdD4LSM5Jv4yXLBqho2amEHmav1ODKz4cNsuxjCi6uaalMvL4u 1a14Xm8Brj8dRKgbR/hUMOWkgmb6fUSWnBstke/U5peUyHVeHrNTVtiK12iCBb/I NVQ2LUhvtWDU1t/2moUc/9cItbH3d9D7ot+lzDFGPMQdKjjqHWYDffzbg7jlUWT0 8eKTikyVDZzXRrmssDEblOQt4oL4/V1tP83xPsM02YQyeCyZk95eNILu/IumT4qM XiivF+xChPucDhK58hvfQHSkiuZdlDpFn+9Uhi0mq+JnNIYO/c95GiyfDdvl2b+h KeqHehqX1Ikuk/GGXpMpQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeeluddguddtvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtgfesthhqredtreerjeenucfhrhhomhepfdfn rghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrd gtohhmqeenucggtffrrghtthgvrhhnpeffffffjeffudfggeevvdeitdetvdfgjefffeff jeelfeejteevheeghffhvdfgleenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 40EA51700089; Thu, 1 Jun 2023 12:30:07 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-447-ge2460e13b3-fm-20230525.001-ge2460e13 Mime-Version: 1.0 Message-ID: <59ab68ce-8634-4ec5-92ae-ea46a04d774f@app.fastmail.com> In-Reply-To: <97308c1d-e9f0-e28d-78ba-11da2a136ee6@fide.pl> References: <97308c1d-e9f0-e28d-78ba-11da2a136ee6@fide.pl> Date: Thu, 01 Jun 2023 16:29:46 +0000 To: "php internals" 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: larry@garfieldtech.com ("Larry Garfield") On Thu, Jun 1, 2023, at 4:02 PM, Janusz Szczypka wrote: > Dear PHP Internals, > > I would like to propose new functions to search in arrays: array_find(= ) and > array_find_key(). > > array_find(): This function would allow developers to find the first=20 > element in > an array that satisfies a specific condition. The condition would be=20 > defined by > a callback function. The functionality would be similar to the existing > array_filter() function but would instead return the first element tha= t=20 > matches > the callback condition, rather than all matching elements. > This function can be used in a way simliar to in_array with added=20 > functionality > of custom check. > > array_find_key(): This function would return the key of the first=20 > element in an > array that matches a given condition, defined by a callback function. = This > function can be used in a way simliar to array_search with added=20 > functionality > of custom check. > > Both functions stop processing after first match is found, so any side=20 > effects > of the callbacks should be avoided. > > Here are the proposed function definitions: > > /** > =C2=A0* @param callable(mixed):bool $callback > =C2=A0*/ > function array_find(callable $callback, array $array) > { > =C2=A0=C2=A0=C2=A0 foreach ($array as $row) { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (call_user_func($callba= ck, $row) =3D=3D=3D true) { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 re= turn $row; > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } > =C2=A0=C2=A0=C2=A0 } > =C2=A0=C2=A0=C2=A0 return null; > } > > /** > =C2=A0* @param callable(mixed):bool $callback > =C2=A0* @return null|int|string > =C2=A0*/ > function array_find_key(callable $callback, array $array) > { > =C2=A0=C2=A0=C2=A0 foreach($array as $key =3D> $row) { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (call_user_func($callba= ck, $row) =3D=3D=3D true) { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 re= turn $key; > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } > =C2=A0=C2=A0=C2=A0 } > =C2=A0=C2=A0=C2=A0 return null; > } > > Both functions are easy to implement in PHP (and probably as easy to=20 > implement > in C), but IMHO could be nice addition to set of array related functio= ns=20 > in PHP. > > If the response to this e-mail will be positive, I will prepare RFC. > We should also discuss order of arguments as the functions are similar=20 > to both > array_search/in_array with condition-array and array_filter with > array-condition, but as you can see above I am leaning towards=20 > condition-array. > > Looking forward to hearing your thoughts. > > Best regards, > Janusz As with the array_group thread: What is the argument to put these functions in hard to update C, rather = than PHP that is easy to download with Composer? "could be a nice addit= ion" is not a compelling argument. Is it substantially faster? It is t= ruly universally used? Does it correct some security concern? Those are the questions that stdlib additions need to answer. --Larry Garfield