php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79399 array_map preserves keys if array_unique is used before
Submitted: 2020-03-20 16:26 UTC Modified: 2020-03-20 17:05 UTC
From: nemanjac85 at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.3Git-2020-03-20 (Git) OS: Ubuntu 18.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
41 - 34 = ?
Subscribe to this entry?

 
 [2020-03-20 16:26 UTC] nemanjac85 at gmail dot com
Description:
------------
Function array map should produce a new numeric array containing values returned from the callback. This works fine most of the time but apparently if input array_unique is called on input array before calling array_map the new array preserves the numeric indexes.

This further causes issues with json_encode and instead of array of values it encodes the objects with numeric indexes as json object keys. 

Calling array_values before json_encode or array_map solves the issue in userland but I think this breaks the specification of array_map.

Test script:
---------------
https://gist.github.com/n-e-m-a-nj-a/cd5a9a5af281e5a912c60dcc7c18227e

Expected result:
----------------
{
    "product_ids": [
        "2d0f7178-9cec-4dbe-9de2-89005735d595",
        "c45152d2-b6c8-4e8f-a113-c6006dad41d9",
        "dcb4caa4-58a5-424a-a110-cff0f3417a86",
        "f6696454-0d05-4975-b731-1d1201c5de46",
        "7aa45f2e-974c-4340-bf10-72786d554a8b",
        "e3303bfa-36c0-4526-94fb-6a6a0b5a814b",
        "7dfc4796-8784-4918-869d-3bddf0a58d30",
        "638f65f3-2a77-4c74-9abf-4d026a81fd65",
        "cac84617-b5fb-4651-9d40-19ff78ff2e72",
        "1a6c9a25-6f37-4e7a-87de-09c88e5f5a4a",
        "d178d85a-64be-48eb-985d-ddb2901a369b",
        "6fb1bc0a-5fe6-4807-a396-6fe3264c5b49",
        "9843d152-6105-476e-868c-3ca6b7353712",
        "dbb093de-141c-47ca-be91-ad475bb6e69d"
    ]
}

Actual result:
--------------
{
    "product_ids": {
        "0": "2d0f7178-9cec-4dbe-9de2-89005735d595",
        "1": "c45152d2-b6c8-4e8f-a113-c6006dad41d9",
        "2": "dcb4caa4-58a5-424a-a110-cff0f3417a86",
        "3": "f6696454-0d05-4975-b731-1d1201c5de46",
        "4": "7aa45f2e-974c-4340-bf10-72786d554a8b",
        "6": "e3303bfa-36c0-4526-94fb-6a6a0b5a814b",
        "7": "7dfc4796-8784-4918-869d-3bddf0a58d30",
        "8": "638f65f3-2a77-4c74-9abf-4d026a81fd65",
        "9": "cac84617-b5fb-4651-9d40-19ff78ff2e72",
        "10": "1a6c9a25-6f37-4e7a-87de-09c88e5f5a4a",
        "11": "d178d85a-64be-48eb-985d-ddb2901a369b",
        "14": "6fb1bc0a-5fe6-4807-a396-6fe3264c5b49",
        "15": "9843d152-6105-476e-868c-3ca6b7353712",
        "17": "dbb093de-141c-47ca-be91-ad475bb6e69d"
    }
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-20 16:31 UTC] peehaa@php.net
-Status: Open +Status: Not a bug
 [2020-03-20 16:31 UTC] peehaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

> The returned array will preserve the keys of the array argument if and only if exactly one array is passed.
 [2020-03-20 17:05 UTC] requinix@php.net
Would the behavior make more sense if I pointed out that array_unique also preserves keys?
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu May 06 15:01:25 2021 UTC