Bug #79399 [NEW]: array_map preserves keys if array_unique is used before
From: nemanjac85 at gmail dot com
Operating system: Ubuntu 18.04
PHP version: 7.3Git-2020-03-20 (Git)
Package: Arrays related
Bug Type: Bug
Bug description:array_map preserves keys if array_unique is used before
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 bug report at https://bugs.php.net/bug.php?id=79399&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79399&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79399&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79399&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79399&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79399&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79399&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79399&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79399&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79399&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79399&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79399&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79399&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79399&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79399&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79399&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79399&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79399&r=mysqlcfg
Thread (3 messages)
- nemanjac85 at gmail dot com