Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116105 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44139 invoked from network); 20 Sep 2021 09:21:51 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 20 Sep 2021 09:21:51 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 743DC1804C0 for ; Mon, 20 Sep 2021 03:02:32 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 20 Sep 2021 03:02:29 -0700 (PDT) Received: by mail-qk1-f173.google.com with SMTP id f130so1139767qke.6 for ; Mon, 20 Sep 2021 03:02:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=p5r4TMHxPjoL0Yjh4uMuBm6U2EJ857J+JHa8f2MkeH0=; b=LVS/DE5eCe2nmRkuQ5evPWV7qNIDm/Q8a5HPblZhxVokbyV+y3W6r4+3kT8S2/M0m5 Asjm8vyy77JhJwepKcmBiG28Wy24mqs3W01NIzJxWbRMc/WpexQgDMP5CF9ojZWuEU2o XxMldf4Max0EP0Y/JAHB72Ul8IlHM4TQLruOzeACz0kJR+T26gw5nB8aFOqVj967/E2q kxpWFS/Y92a+abhfKsruUiz7lkP89oHP7mv1+e5j33R39rgIqbG0KWpeX4o2E73NLzba RigZLeMUxmd7r4IR7TiBfzYJJoAUgkwFJhHgAkfReQIkECzF1Ky5Y9REHbLDp7wKlQ5Z BBXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=p5r4TMHxPjoL0Yjh4uMuBm6U2EJ857J+JHa8f2MkeH0=; b=e4JHmE45dCfT9nzwhzfxd4XXsCZzG8ci4I2dfIk8Ij9+KMszugQoDqcZ1W5Ai9jgO3 O5cKxu2DaCOZE8cotBZO/dLVxlBCP+OsnCb19lMylQiuGWnaEHSLVLN+zvKqTB38lOmt MGIpkoKj7Xo0l61PFnYpZhlQZYBEOWtmwyr8Lyi71cBV33CfuihMen86FbZ8GqJQI/eb 7xpu9o5Gv6RXIlvGaewvGeJw1+ECkhW1amImwopuUZAy9nfLrUPPoQQprTZOXSSLUidS oVvPskc/a4lTHrn0CvSEt9GtM3It9AG4gtv/lD4XqZBlMKGMlVpi3qkaWHZKMBj3PL9+ de5w== X-Gm-Message-State: AOAM533LddlQmE+QoY4tYMB0ElxgYgY96eQ4R9bT7rozhogID/lHV6oh M9Q812iGKayUvnfpm7FDQgXHnWIfVw6BJlx4n9Q= X-Google-Smtp-Source: ABdhPJwjUslhZT2Tf+qZiUIxxu+5tlTpp+Xv8elZ/db23hjFaTpLU6aHYSN/f5DgeVMlDXs7jCJHIDyT4yV/2472lEU= X-Received: by 2002:a25:938e:: with SMTP id a14mr29735505ybm.144.1632132143810; Mon, 20 Sep 2021 03:02:23 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 20 Sep 2021 13:02:12 +0300 Message-ID: To: tyson andre Cc: PHP Internals Content-Type: multipart/alternative; boundary="000000000000a8a33805cc6a6008" Subject: Re: [PHP-DEV] Proposal: Adding an ARRAY_FILTER_REINDEX flag to array_values From: zsidelnik@gmail.com (Eugene Sidelnyk) --000000000000a8a33805cc6a6008 Content-Type: text/plain; charset="UTF-8" Hi, I myself faced such bugs because filter function preserves keys. From my experience it is not that easy to locate bug like this. In my case I rewrote solution in other way than it was originally written. Only later I realized that root cause was array_filter On Sun, Sep 19, 2021, 4:11 PM tyson andre wrote: > Hi internals, > > Currently, array_filter will always return the original keys. > This often requires an additional wrapping call of > array_values(array_filter(...)) to reindex the keys and return a list. > (or applications may not realize there will be gaps in the keys until it > causes a bug or unexpected JSON encoding, etc.) > > PHP is also more memory/time efficient at creating packed arrays than it > is at creating associative arrays. > > What are your thoughts on adding `ARRAY_FILTER_REINDEX`, to ignore the > original int/string keys and replace them with `0, 1, 2, ...` > > ``` > php > echo json_encode(array_filter([5,6,7,8], fn($value) => $value % 2 > > 0)); > {"0":5,"2":7} > // proposed flag > php > echo json_encode(array_filter([5,6,7,8], fn($value) => $value % 2 > > 0, ARRAY_FILTER_REINDEX)); > [5,7] > ``` > > https://www.php.net/array_filter already has the `int $mode = 0` which > accepts the bit flags `ARRAY_FILTER_USE_KEY` and `ARRAY_FILTER_USE_BOTH` > These could be then be combined with the proposed bit flag > `ARRAY_FILTER_REINDEX`, e.g. to filter an array based on both the array > keys and values, and return a list without gaps. > (and if $callback is null, this would return a list containing only the > truthy values) > > Thoughts? > > Thanks, > Tyson > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --000000000000a8a33805cc6a6008--