Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119493 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53216 invoked from network); 8 Feb 2023 16:50:36 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Feb 2023 16:50:36 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4C72418037E for ; Wed, 8 Feb 2023 08:50:35 -0800 (PST) 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,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 176.9.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (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 ; Wed, 8 Feb 2023 08:50:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1675875033; bh=Bj4PXO+FZnxhu34yFRKMMUnuYPL2Wg1vkyHz3I2UmE0=; h=Date:Subject:To:References:From:In-Reply-To:From; b=OMr88uKu+OU/T88/awhviAPJFAaw7Tk4dplhzH+Lpp6CtCh2Hzv7EVzB2vy/75L1C lsVwCdo1XXNMVtO90DXA5b9BsXT4Au5SyjQXINYHJ0wuu92XdoYrvjSjbqArbG7yon fjtA+5BKgOnspLSGUQTCPOxr8SQB8qJvYaolaZlCcOBJbn22LX/eXNSE0DOCQ1TFwa f02JOlWsOlT4G91E7Pk9ZI04G3Vm5xbnE07ymdcdu7CnDtq09qd+AHjMf4MkswJYsi 3PW5MSSvEwyvnkZiuIL1Q87oHkm3DXauAlszMXhCcXNa3/aFw9SqqzXft8a9EkLxSr 0+lozQOazyfZQ== Message-ID: <30c259c7-1803-4542-2501-216028ac7a81@bastelstu.be> Date: Wed, 8 Feb 2023 17:50:32 +0100 MIME-Version: 1.0 Content-Language: en-US To: Sergii Shymko , "internals@lists.php.net" References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] RFC proposal: function array_filter_list() to avoid subtle bugs/workarounds From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=c3=bcsterhus?=) Hi On 2/1/23 18:21, Sergii Shymko wrote: > For example: > $originalList = ['first', '', 'last']; > $filteredList = array_filter($originalList); > var_export(filteredList); // array(0 => 'first', 2 => 'last') > var_export(array_is_list($originalList)); // true > var_export(array_is_list($filteredList)); // false > > The behavior is counterintuitive and can lead to subtle bugs, such as encoding issues: > echo json_encode($originalList); // ["first", "", "last"] > echo json_encode($filteredList); // {"0": "first", "2": "last"} > > The workaround is to post-process the filtered array with array_values() to reset the indexes. > The proposal is to introduce a function array_filter_list() that would work solely on lists. > It will have the same signature as array_filter() and will always return a valid list. I agree with the general premise that "array_filter turns lists into non-lists" is not great and I've encountered the issue myself more than I would like. However I also agree with the other replies that array_filter_list() is likely not the correct solution. I believe that defaults matter and as a developer when I have the choice between 'array_filter' and 'array_filter_list', the former is likely going to be the default, because it's shorter. So instead of remembering to wrap the array_filter() into array_values(), I have to remember which of the two functions I need. I also believe that non-array iterables should not be a second class citizen to arrays and would like to point to my email in this mailing list thread for the "[List/Assoc\unique]" RFC and thus a pretty similar topic, because it also applies here: https://externals.io/message/119070#119072 Perhaps the hypothetical "iterable\filter" function could be smart with regard to the handling of keys? If the input appears to be consecutively indexed, it is treated as a list and reindexed. Or it could default to dropping the keys, which I consider the better default, because dropping keys is *immediately* obvious when testing, whereas having holes in a list is not. Best regards Tim Düsterhus