3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = array( array("title" => 1, "children" => array( array( "selected" => true, "key" => 100, "children" => array() ) ), array( "selected" => true, "key" => 500, "children" => array() ), array( "selected" => true, "key" => 200, "children" => array( array( "selected" => true, "key" => 300, "children" => array( array( "selected" => true, "key" => 400, "children" => array() ) ) ) ) ) ) ); $selectedValues = array(); $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($array)); foreach ($iterator as $key => $value) { if ($key == "key") { echo $value .PHP_EOL; var_dump($iterator->getChildren()); } } /* $selectedValues = array(); $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($array)); foreach ($iterator as $key => $value) { if ($key == "selected") { $depth = $iterator->getDepth(); $subIterator = $iterator->getSubIterator($depth); foreach ($subIterator as $key2 => $value2) { if ($key2 == "key") { echo $value2 . PHP_EOL; } } } } */
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
100 Fatal error: Uncaught TypeError: ArrayIterator::__construct(): Argument #1 ($array) must be of type array, int given in /in/4gM9I:40 Stack trace: #0 [internal function]: ArrayIterator->__construct(100, 0) #1 /in/4gM9I(40): RecursiveArrayIterator->getChildren() #2 {main} thrown in /in/4gM9I on line 40
Process exited with code 255.
Output for 7.0.20, 7.1.5 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
100 Fatal error: Uncaught InvalidArgumentException: Passed variable is not an array or object in /in/4gM9I:40 Stack trace: #0 [internal function]: ArrayIterator->__construct(100, 0) #1 /in/4gM9I(40): RecursiveArrayIterator->getChildren() #2 {main} thrown in /in/4gM9I on line 40
Process exited with code 255.
Output for 7.0.0 - 7.0.6, 7.1.0
100 Fatal error: Uncaught InvalidArgumentException: Passed variable is not an array or object, using empty array instead in /in/4gM9I:40 Stack trace: #0 [internal function]: ArrayIterator->__construct(100, 0) #1 /in/4gM9I(40): RecursiveArrayIterator->getChildren() #2 {main} thrown in /in/4gM9I on line 40
Process exited with code 255.
Output for 5.4.2 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
100 Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in /in/4gM9I:40 Stack trace: #0 [internal function]: ArrayIterator->__construct(100, 67108864) #1 /in/4gM9I(40): RecursiveArrayIterator->getChildren() #2 {main} thrown in /in/4gM9I on line 40
Process exited with code 255.

preferences:
222.5 ms | 402 KiB | 257 Q