<?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>';
You have javascript disabled. You will not be able to edit any code.