3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Person { var $name; var $age; function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $a = new ArrayIterator([ new Person("Christoph", 15), new Person("John", 42), new Person("Foobar", 25) ]); function first_match(\Iterator $iterator, callable $callback) { foreach ($iterator as $key => $value) { if ($callback($value, $key)) { yield $key => $value; break; } } } $firstOver18 = function($person) { return $person->age >= 18; }; var_dump(first_match($a, $firstOver18)->current());

preferences:
40.44 ms | 402 KiB | 5 Q