Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Array map preserves keys if array_unique is called beforehand
<?php
final class ProductId
{
private $id;
public function __construct(string $id)
{
$this->id = $id;
}
public static function fromString(string $id): self
{
return new self($id);
}
public function __toString()
{
return $this->id;
}
}
$productIds = [
ProductId::fromString("2d0f7178-9cec-4dbe-9de2-89005735d595"),
ProductId::fromString("c45152d2-b6c8-4e8f-a113-c6006dad41d9"),
ProductId::fromString("dcb4caa4-58a5-424a-a110-cff0f3417a86"),
ProductId::fromString("f6696454-0d05-4975-b731-1d1201c5de46"),
ProductId::fromString("7aa45f2e-974c-4340-bf10-72786d554a8b"),
ProductId::fromString("7aa45f2e-974c-4340-bf10-72786d554a8b"),
ProductId::fromString("e3303bfa-36c0-4526-94fb-6a6a0b5a814b"),
ProductId::fromString("7dfc4796-8784-4918-869d-3bddf0a58d30"),
ProductId::fromString("638f65f3-2a77-4c74-9abf-4d026a81fd65"),
ProductId::fromString("cac84617-b5fb-4651-9d40-19ff78ff2e72"),
ProductId::fromString("1a6c9a25-6f37-4e7a-87de-09c88e5f5a4a"),
ProductId::fromString("d178d85a-64be-48eb-985d-ddb2901a369b"),
ProductId::fromString("d178d85a-64be-48eb-985d-ddb2901a369b"),
ProductId::fromString("d178d85a-64be-48eb-985d-ddb2901a369b"),
ProductId::fromString("6fb1bc0a-5fe6-4807-a396-6fe3264c5b49"),
ProductId::fromString("9843d152-6105-476e-868c-3ca6b7353712"),
ProductId::fromString("9843d152-6105-476e-868c-3ca6b7353712"),
ProductId::fromString("dbb093de-141c-47ca-be91-ad475bb6e69d"),
];
$unique = array_unique($productIds);
$res = [];
$res['product_ids'] = \array_map(function (ProductId $productId) {
return (string) $productId;
}, $unique);
$json = json_encode($res, JSON_PRETTY_PRINT);
echo $json;
/**
* //Expected
* {
* "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"
* ]
* }
*
* //Getting
* {
* "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"
* }
*}
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment