3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Person { public $id; public $name; public $phones = []; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } public function addPhone($phone) { $this->phones[] = $phone; } } /** * @return Person[] */ function statementToPersonList(iterable $stmt): array { $personList = []; foreach ($stmt as $row) { $id = $row['id']; $name = $row['name']; $number = $row['number']; if (!isset($personList[$name])) { $personList[$name] = new Person($id, $name); } $personList[$name]->addPhone($number); } return $personList; } $personList = statementToPersonList([ ['id' => 1, 'name' => 'Alice', 'number' => 'n1'], ['id' => 2, 'name' => 'Alice', 'number' => 'n2'], ['id' => 3, 'name' => 'Bob', 'number' => 'n3'], ['id' => 4, 'name' => 'Carol', 'number' => 'n4'], ['id' => 5, 'name' => 'Alice', 'number' => 'n5'], ]); print_r($personList);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [Alice] => Person Object ( [id] => 1 [name] => Alice [phones] => Array ( [0] => n1 [1] => n2 [2] => n5 ) ) [Bob] => Person Object ( [id] => 3 [name] => Bob [phones] => Array ( [0] => n3 ) ) [Carol] => Person Object ( [id] => 4 [name] => Carol [phones] => Array ( [0] => n4 ) ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
27.87 ms | 407 KiB | 5 Q