<?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
);
preferences:
23.69 ms | 406 KiB | 5 Q