Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120500 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53214 invoked from network); 1 Jun 2023 16:02:20 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Jun 2023 16:02:20 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2B5F91804D4 for ; Thu, 1 Jun 2023 09:02:20 -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_40,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8075 104.40.0.0/13 X-Spam-Virus: No X-Envelope-From: Received: from azure.fide.pl (azure.fide.pl [104.45.0.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 1 Jun 2023 09:02:20 -0700 (PDT) Received: from [10.10.0.41] (94-172-157-17.dynamic.chello.pl [94.172.157.17]) by azure.fide.pl (Postfix) with ESMTPSA id E2062FDAE5 for ; Thu, 1 Jun 2023 18:02:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=fide.pl; s=mail; t=1685635338; bh=HRNouVl2e+kSNYExowACeTce74B5RoNrcdcw/YoBiLQ=; h=Date:To:From:Subject:From; b=3MYzsBoiNKc9qFHz1slfSUP+HeGk6LJTBfCHGZ1PAjwwhvcFvGVIzdGcXqWwh8t7N eRgegQrI/kbBCMUoa/OTJBIm0RdDS33eTDBciXcv+rldekvHJoEHFbYMG3/T/MrqYx MNpw+8I90BLYIDm+7nNzL5dLMjgIts7XI6DnIgC8= Message-ID: <97308c1d-e9f0-e28d-78ba-11da2a136ee6@fide.pl> Date: Thu, 1 Jun 2023 18:02:18 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.11.1 Content-Language: pl To: internals@lists.php.net Organization: FIDE.pl Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Proposal: addition of array_find() and array_find_key() functions From: janusz@fide.pl (Janusz Szczypka) 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 element in an array that satisfies a specific condition. The condition would be defined by a callback function. The functionality would be similar to the existing array_filter() function but would instead return the first element that matches the callback condition, rather than all matching elements. This function can be used in a way simliar to in_array with added functionality of custom check. array_find_key(): This function would return the key of the first 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 functionality of custom check. Both functions stop processing after first match is found, so any side effects of the callbacks should be avoided. Here are the proposed function definitions: /**  * @param callable(mixed):bool $callback  */ function array_find(callable $callback, array $array) {     foreach ($array as $row) {         if (call_user_func($callback, $row) === true) {             return $row;         }     }     return null; } /**  * @param callable(mixed):bool $callback  * @return null|int|string  */ function array_find_key(callable $callback, array $array) {     foreach($array as $key => $row) {         if (call_user_func($callback, $row) === true) {             return $key;         }     }     return null; } Both functions are easy to implement in PHP (and probably as easy to implement in C), but IMHO could be nice addition to set of array related functions 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 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 condition-array. Looking forward to hearing your thoughts. Best regards, Janusz