3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo '<pre>'; $id = 1035; // Searching ID $myObj = array(); $a = [ 'id'=> 291, 'children' => [ [ 'id' => 1034, 'children' => [ [ 'id' => 111, 'name' => 'ABC', 'figure' => '6 digits', 'children'=> [] ], [ 'id' => 1035, 'lft' => 'LEFT', 'children' => [ [ 'id' => 1036, 'children' => [ [ 'id' => 222, 'someKey' => 'some value', 'children'=> [] ] ] ], [ 'id' => 333, 'someKey' => 'some value', 'children'=> [] ] ], ] ], ], [ 'id' => 1024, 'title' => 'ABC', 'children' => [ ], ] ] ]; function findObject($id, $obj) { global $myObj; // This is an object. if (isset($obj["id"])) { echo "Checking {$obj["id"]}<br />"; // Check the id to what we need. if ($obj["id"] == $id) { // Yay! We found it. Return the object. echo "<strong>Yay we found {$obj["id"]}</strong><br />"; $myObj = $obj; echo "<strong>Need to find a way to break out!</strong><br />"; } else { echo "Checking children of {$obj["id"]}<br />"; // See if it has any children if (isset($obj["children"]) && count($obj["children"]) > 0) { echo "There are children for {$obj["id"]}<br />"; foreach ($obj["children"] as $child) { findObject($id, $child); } } } } } findObject($id, $a); if (isset($myObj) && !empty($myObj)) { echo "<br /><strong>Found it!</strong><br />"; print_r($myObj); } else echo "Sorry, can't find the ID!"; echo '</pre>';
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
<pre>Checking 291<br />Checking children of 291<br />There are children for 291<br />Checking 1034<br />Checking children of 1034<br />There are children for 1034<br />Checking 111<br />Checking children of 111<br />Checking 1035<br /><strong>Yay we found 1035</strong><br /><strong>Need to find a way to break out!</strong><br />Checking 1024<br />Checking children of 1024<br /><br /><strong>Found it!</strong><br />Array ( [id] => 1035 [lft] => LEFT [children] => Array ( [0] => Array ( [id] => 1036 [children] => Array ( [0] => Array ( [id] => 222 [someKey] => some value [children] => Array ( ) ) ) ) [1] => Array ( [id] => 333 [someKey] => some value [children] => Array ( ) ) ) ) </pre>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 <pre>Checking 291<br />Checking children of 291<br />There are children for 291<br />Checking 1034<br />Checking children of 1034<br />There are children for 1034<br />Checking 111<br />Checking children of 111<br />Checking 1035<br /><strong>Yay we found 1035</strong><br /><strong>Need to find a way to break out!</strong><br />Checking 1024<br />Checking children of 1024<br /><br /><strong>Found it!</strong><br />Array ( [id] => 1035 [lft] => LEFT [children] => Array ( [0] => Array ( [id] => 1036 [children] => Array ( [0] => Array ( [id] => 222 [someKey] => some value [children] => Array ( ) ) ) ) [1] => Array ( [id] => 333 [someKey] => some value [children] => Array ( ) ) ) ) </pre>

preferences:
132 ms | 405 KiB | 205 Q