<?php
class Item
{
private $id;
private $name;
function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
public function __get($prop)
{
return $this->$prop;
}
}
$itemList = [
new Item(1, "Item 1"),
new Item(2, "Item 2"),
new Item(3, "Item 3"),
new Item(4, "Item 4")
];
echo implode(',', array_map(fn($obj) => $obj->id, $itemList));
echo "\n---\n";
echo array_reduce(
$itemList,
fn($result, $obj) => $result . ($result ? ',' : '') . $obj->id
);
- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- 1,2,3,4
---
1,2,3,4
preferences:
74.96 ms | 406 KiB | 5 Q