3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); final class PathAsKeyDecorator implements \Iterator { private RecursiveIteratorIterator $inner; public function __construct(RecursiveIteratorIterator $inner) { $this->inner = $inner; } public function current() { return $this->inner->current(); } public function next(): void { $this->inner->next(); } public function key() { $path = []; for ($i = 0, $depth = $this->inner->getDepth(); $i <= $depth; $i++) { $path[] = $this->inner->getSubIterator($i)->key(); } return $path; } public function valid(): bool { return $this->inner->valid(); } public function rewind(): void { $this->inner->rewind(); } } $input = [ 'steve' => [ 'id' => [ '#text' => 1, ], ], 'albert' => [ 'id' => [ '#text' => 2, ], ], 'john' => [ 'profil' => [ 'id' => [ '#text' => 3, ], ], ], ]; // this is the filter function that should be customized given your requirements // or create a factory function which produces these types of filter functions $filter = static function ($current, array $path): bool { // with help from the PathAsKeyDecorator // we can decide on the path to the current value return ['id', '#text'] === array_slice($path, -2) // and the current value && 2 === $current; }; // configure the iterator $it = new CallbackFilterIterator( new PathAsKeyDecorator(new RecursiveIteratorIterator(new RecursiveArrayIterator($input))), $filter, ); // traverse the iterator foreach ($it as $path => $val) { print_r([ 'path' => $path, 'val' => $val ]); }
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of PathAsKeyDecorator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/fmrAk on line 13 Deprecated: Return type of PathAsKeyDecorator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/fmrAk on line 23 Array ( [path] => Array ( [0] => albert [1] => id [2] => #text ) [val] => 2 )

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:
119.56 ms | 407 KiB | 5 Q