fork download
  1. <?php
  2. echo '<pre>';
  3. $id = 1035; // Searching ID
  4. $myObj = array();
  5.  
  6. $a = [
  7. 'id'=> 291,
  8. 'children' => [
  9. [
  10. 'id' => 1034,
  11. 'children' => [
  12. [
  13. 'id' => 111,
  14. 'name' => 'ABC',
  15. 'figure' => '6 digits',
  16. 'children'=> []
  17. ],
  18. [
  19. 'id' => 1035,
  20. 'lft' => 'LEFT',
  21. 'children' => [
  22. [
  23. 'id' => 1036,
  24. 'children' => [
  25. [
  26. 'id' => 222,
  27. 'someKey' => 'some value',
  28. 'children'=> []
  29. ]
  30. ]
  31. ],
  32. [
  33. 'id' => 333,
  34. 'someKey' => 'some value',
  35. 'children'=> []
  36. ]
  37. ],
  38. ]
  39. ],
  40. ],
  41. [
  42. 'id' => 1024,
  43. 'title' => 'ABC',
  44. 'children' => [
  45.  
  46. ],
  47. ]
  48. ]
  49. ];
  50.  
  51. function findObject($id, $obj) {
  52. global $myObj;
  53. // This is an object.
  54. if (isset($obj["id"])) {
  55. echo "Checking {$obj["id"]}<br />";
  56. // Check the id to what we need.
  57. if ($obj["id"] == $id) {
  58. // Yay! We found it. Return the object.
  59. echo "<strong>Yay we found {$obj["id"]}</strong><br />";
  60. $myObj = $obj;
  61. echo "<strong>Need to find a way to break out!</strong><br />";
  62. }
  63. else {
  64. echo "Checking children of {$obj["id"]}<br />";
  65. // See if it has any children
  66. if (isset($obj["children"]) && count($obj["children"]) > 0) {
  67. echo "There are children for {$obj["id"]}<br />";
  68. foreach ($obj["children"] as $child) {
  69. findObject($id, $child);
  70. }
  71. }
  72. }
  73. }
  74. }
  75.  
  76. findObject($id, $a);
  77. if (isset($myObj) && !empty($myObj)) {
  78. echo "<br /><strong>Found it!</strong><br />";
  79. print_r($myObj);
  80. } else
  81. echo "Sorry, can't find the ID!";
  82.  
  83. echo '</pre>';
Success #stdin #stdout 0.02s 23844KB
stdin
Standard input is empty
stdout
<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>