Bug #79399 [Nab]: array_map preserves keys if array_unique is used before
Edit report at https://bugs.php.net/bug.php?id=79399&edit=1
ID: 79399
Updated by: requinix@php.net
Reported by: nemanjac85 at gmail dot com
Summary: array_map preserves keys if array_unique is used
before
Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Ubuntu 18.04
PHP Version: 7.3Git-2020-03-20 (Git)
Block user comment: N
Private report: N
New Comment:
Would the behavior make more sense if I pointed out that array_unique also preserves keys?
Previous Comments:
------------------------------------------------------------------------
[2020-03-20 16:31:08] 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 16:26:18] 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"
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79399&edit=1
Thread (3 messages)