Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112753 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 38442 invoked from network); 4 Jan 2021 22:08:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Jan 2021 22:08:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0710E1804E4 for ; Mon, 4 Jan 2021 13:44:56 -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,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from NAM04-SN1-obe.outbound.protection.outlook.com (mail-oln040092011016.outbound.protection.outlook.com [40.92.11.16]) (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 ; Mon, 4 Jan 2021 13:44:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g++u7wS++FvEgCJxnv4Ycj48J/QOE8duN1Wv1B2j2vO7p7kKH+m4SiLOQqOG7kYqMd7qHnWNVeCxMcIKXlzCU5szQYIkMmgN3GBeOkWZ0z9Zu2FOtRXj7LTILBdxvoV6fEYKAKBU2Blf5je9tweIE/RoOa1d3Z47jDkJ+BlQ4JgVJKiMqh0bwT3ACVgND4NeTD6SWyZI6TZ9RTIKQbG52NIrjjSP3Y7VxMwupxyFk1hHUaXju74kqvKOVJLzeBgpUhCjcTokDp5x2mAXYvNAwH+B7AcUwlcLLmn0Hc9mDRxiKehlswJvIbboEhgRIPl52fE+e9F/d9h6hy+9Lz1noA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZffPqNApLPuG5xs4Tsybt2Kcwf3ZXjWXIPyqBU+DKWA=; b=oeK6SoWHEVEpKaRkWSqHysHix+WRYq6MGfeu9+W51SnlAjHwqp/JGWc9NlwKYMNsxkitvFl0SG+AQIlGmwfyVs1uU2RtlBvfwKK5L43d1D3NBOIg/2fhfDisy4PS+SxyVfGfhabg+Q2GujertQS4XscaPj9pdDF/aiYQp8ARtQf433rCoqwrYPHpIUb0v8YGZgtZNXGLzaX7mL2pBdPgKCRP+OP52kU/A539817DCG4ErgAmip5ztKI9fFXtDTYmos50erOHS71ZyBWq1QbvvkzQ2/aCONawGJJtiMC2S6eXTsMTBG05+kYKlwEQzu4Eq2tWjfZBtLJoAQ+2j/1DcQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZffPqNApLPuG5xs4Tsybt2Kcwf3ZXjWXIPyqBU+DKWA=; b=RLM6cqm/+MndfoUgv+ya7cd0pIcexxjodghg6aJPv0/R+DNHe9dciYOtHgkkLp9rJN+nBX2x0OfTseQGfTj3wkVlviIzQ/+STaT3j5ZUIfQlr3Xf467b01T5YS7nsWkApVNB0jMHh0b1dpH9tqMlkFRPwDxkktbdc/L4h0mpdmXkgga4wrTfEcM96RDEzEW7eQodAMZdAQtoa7aVRo1SLaQ9EayZ2Yy8KS5BSOS43e76ShBXv65auUliW8r/jM5jHhVNEqsSOY4WBqrTVPpOqQ4jq4LeFZTkQ3RSf2VqBIM1x+kdZ7WCytWrxBWBlq8VJ1C2sorn4toRUP1K5vnxjw== Received: from BN8NAM04FT029.eop-NAM04.prod.protection.outlook.com (2a01:111:e400:7e85::46) by BN8NAM04HT258.eop-NAM04.prod.protection.outlook.com (2a01:111:e400:7e85::475) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3721.23; Mon, 4 Jan 2021 21:44:50 +0000 Received: from BYAPR05MB5478.namprd05.prod.outlook.com (2a01:111:e400:7e85::4e) by BN8NAM04FT029.mail.protection.outlook.com (2a01:111:e400:7e85::342) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3721.23 via Frontend Transport; Mon, 4 Jan 2021 21:44:50 +0000 Received: from BYAPR05MB5478.namprd05.prod.outlook.com ([fe80::8c94:5632:4960:d76a]) by BYAPR05MB5478.namprd05.prod.outlook.com ([fe80::8c94:5632:4960:d76a%6]) with mapi id 15.20.3742.006; Mon, 4 Jan 2021 21:44:50 +0000 To: php internals , tyson andre Thread-Topic: [PHP-DEV] Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays Thread-Index: AQHW1m7vogEIYJkiwEyrC80th3/gm6oWSyBggACSbgCAATCMVQ== Date: Mon, 4 Jan 2021 21:44:50 +0000 Message-ID: References: ,<3fe86a3a-7aaa-48c0-b1fa-2f5266e3bdf2@www.fastmail.com> In-Reply-To: <3fe86a3a-7aaa-48c0-b1fa-2f5266e3bdf2@www.fastmail.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:FD81D73E726D9469A0D8B3CC6B659EEDAA641190912B236FD278D3681612C1D5;UpperCasedChecksum:8CBF70F93671F10ACB702DDEB921B332594962A5770DE6D2D37713F0C71705D2;SizeAsReceived:7126;Count:44 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [vjHvP/KN4tE6qzBVjCwLE+g1A864zxvj] x-ms-publictraffictype: Email x-incomingheadercount: 44 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 7af3e143-091e-4a87-45b0-08d8b0f9f68f x-ms-exchange-slblob-mailprops: =?Windows-1252?Q?/yZTZJE5//w0ALkDlGvC+fJjXRDUHFSlujBQxiKj4i2OeHd/cIZtfjvZ?= =?Windows-1252?Q?r6RiXO/PGMFjspjh8dIhPKt6QUN75KAm9bqn6g396Hbvso7UZLrMhlMq?= =?Windows-1252?Q?zLq8i3yY3HHy2SjYCqc9+GRW671xP5tqS6ytxE6wy+YUV8lvb8bc/Zew?= =?Windows-1252?Q?QCJeJsYFh/2SNoe5bjETOrfJjCNmW1K79qnkBIfasGcP2lzv/MXvIzHk?= =?Windows-1252?Q?k01Pg+1S5FBw4eOMtaWttD8NyVLPXI8lmTcFeeb4uyeDKq47sZcvLcxs?= =?Windows-1252?Q?/A2/fNRSw0H6AAp0ss/ElMOJc2OcRAZOTmYgItKa1fUvzxFngHARYKi5?= =?Windows-1252?Q?lUpMWvrU7X8J+C6kSlR03uzfvXjzS35IC0yLW/G4jP0DlgvYz6LIyc/1?= =?Windows-1252?Q?gTEkIAcMq/GS7bX/zbWYKmb0gyBCF1A/j/QG0oZR/07/HwGliPw1WJEP?= =?Windows-1252?Q?DZOZdPNazebg/se5zBfefhr6nvQBMmes384i0HbuaCBzF2d4HCumheK6?= =?Windows-1252?Q?KO39CSfetjcBuDFqNQqsPLBrzEWw1DtjR5iGsRa6PMgPChKu+SNzj2PL?= =?Windows-1252?Q?+tH4Mt6cD8yEix/mgtq72f9Pw5+P9ML3TpZkDt0S7C5dD3gUtNsPvAa5?= =?Windows-1252?Q?ctf4dcDtFxJQvtIcdd80RgVNw+0BSmKezsoEJt7hAzi3fxamIWrmiJs0?= =?Windows-1252?Q?E7FWF/j9lMQrQoHP09pKvJ9u16yJXq3NUg1OE/DzBxYp+sfnlcYr4dqL?= =?Windows-1252?Q?kb/hv5W9sRiX4dUKJAuA0aBgX4YRrJiuSH0lMmatRTsOULHn/N7e51VW?= =?Windows-1252?Q?RQv56W2jIsgbJF0w/9FswOYydJyZ54KFFYL1Ask6z7dwg262TpGXKe/K?= =?Windows-1252?Q?oq1JluBOYCrMkMGo4Xq9QQum9KAUL/DlnQpfXn9OM+yBbBnBR9Qu78hQ?= =?Windows-1252?Q?5RoelSSGt45tr7MJYUBs4ckUjPnIwyAZI8+F/NvsEeyX5urGbz+BUHZ9?= =?Windows-1252?Q?VINbwsYTROIvsnNQg62tAwnBwbILPzi2A4gSN7zoGKJHFAYDWeLAM9w0?= =?Windows-1252?Q?PAk7xLnUuL+0Hje9RMkdJuboooSib3OjloIZCq1jegqIGQyUno6u40v8?= =?Windows-1252?Q?k1ftC2kKBdHl0dgf8tD0VDLjuc3w+mZtDfEIeS4cq2U+FBewWFQSl1Uu?= x-ms-traffictypediagnostic: BN8NAM04HT258: x-ms-exchange-minimumurldomainage: externals.io#1646 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: CgMvKRBfq8v5UZuAwpefqiHrKB9dJoD1zmdmnvLKyAT1dBg6Pqc9TeqPKdDLJH5ALEYxu60awuKI585cTsrPVOEZ7zuBOXju1V8rvSJXZ4i7UbwT/3vpBDk4Y0OBEGLrL5tuCotyv4BKt7ZqlKq8vbbZ61ULH51PmXxsEs1cjsu3qUlOYeE3JeCtIPGvjJstC36M085my7vxYzRUq8uUjTEEc7MPtdja1Z/XyQz27DN6tizTC5XDuvY4EOCvGDIDZJCTQHFpHcsRe273bclcHbE3+EGM2clyFRdnhd3F0eU= x-ms-exchange-antispam-messagedata: UPbtgb65EZRU4eVkNP7Sd1GozxjRW5V1+nPrjiwwanMsXHwGXav2kUBNWWagnDIDJpP1DVeGoJYOzCzWQDLq8CIb1Dao/lxRr8SDvvq+QXVDb6KJG+hzbDEcWyGhIqhOpW7Y+OMn4Z56/Z9byPj55w== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-AuthSource: BN8NAM04FT029.eop-NAM04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 7af3e143-091e-4a87-45b0-08d8b0f9f68f X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Jan 2021 21:44:50.2405 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8NAM04HT258 Subject: Re: [PHP-DEV] Re: [RFC] Add is_list(mixed $value): bool to check for list-like arrays From: theodorejb@outlook.com (Theodore Brown) On Sun, Jan 3, 2021 at 8:56 PM Larry Garfield wrot= e:=0A= =0A= > On Sun, Jan 3, 2021, at 12:17 PM, tyson andre wrote:=0A= > > > I've created the RFC https://wiki.php.net/rfc/is_list=0A= > > >=0A= > > > This adds a new function `is_list(mixed $value): bool` that will=0A= > > > return true if the type of $value is array and the array keys are=0A= > > > `0 .. count($value)-1` in that order.=0A= > > >=0A= > > > It's well-known that PHP's `array` data type is rare among=0A= > > > programming languages in that it supports both integer and string=0A= > > > keys and that iteration order is important and guaranteed. (it is=0A= > > > used for overlapping use cases - in many other languages, both=0A= > > > vectors/lists/arrays and hash maps are available)=0A= > > >=0A= > > > While it is possible to efficiently check that something is an=0A= > > > array, that array may still have string keys, not start from 0,=0A= > > > have missing array offsets, or contain out of order keys.=0A= > > >=0A= > > > It can be useful to verify that the assumption that array keys=0A= > > > are consecutive integers is correct, both for data that is being=0A= > > > passed into a module or for validating data before returning it=0A= > > > from a module. However, because it's currently inconvenient to do=0A= > > > that, this has rarely been done in my experience.=0A= > > >=0A= > > > In performance-sensitive serializers or data encoders, it may=0A= > > > also be useful to have an efficient check to distinguish lists=0A= > > > from associative arrays. For example, json_encode does this when=0A= > > > deciding to serialize a value as [0, 1, 2] instead of {=930=94:0,=932= =94:1,=931=94:1}=0A= > > > for arrays depending on the key orders.=0A= > > >=0A= > > > Prior email threads/PRs have had others indicate interest in the=0A= > > > ability to efficiently check if a PHP `array` has sequential=0A= > > > ordered keys starting from 0=0A= > > >=0A= > > > https://externals.io/message/109760 =93Any interest in a list type?= =94=0A= > > > https://externals.io/message/111744 =93Request for couple memory=0A= > > > optimized array improvements=94=0A= > > > Implementation: https://github.com/php/php-src/pull/6070=0A= > > > (some discussion is in the linked PR it was based on)=0A= > >=0A= > >=0A= > > Due to concerns about naming causing confusion with theoretical=0A= > > potential future changes to the language,=0A= > > I've updated https://wiki.php.net/rfc/is_list to use the name=0A= > > `is_array_and_list(mixed $value): bool` instead.=0A= > > (e.g. what if php used the reserved word `list` to add an actual list= =0A= > > type in the future, and is_list() returned false for that.)=0A= > >=0A= > > I plan to start voting on the RFC in a few days.=0A= >=0A= >=0A= > Possible alternative that's less clumsy: is_packed_array?=0A= =0A= =0A= Hi Tyson,=0A= =0A= Thanks for the proposal and implementation. I've wanted a function=0A= that does this on numerous occasions, so I think it will be a good=0A= addition to the standard library.=0A= =0A= If I may chime in with Larry's suggestion: I think `is_packed_array`=0A= would a better name than `is_array_and_list`. The latter is potentially=0A= confusing since it sounds like the value has more than one type.=0A= `is_packed_array` makes it clearer that the function checks whether=0A= the value is an array matching certain criteria. I think it also reads=0A= better when used along with other functions. For example:=0A= =0A= function is_associative_array(mixed $value) {=0A= return is_array($value) && !is_packed_array($value);=0A= // vs.=0A= return is_array($value) && !is_array_and_list($value);=0A= }=0A= =0A= Best regards, =0A= Theodore=