title: Get parent values from multidimensional associative json file
body: <p>I have a multi- and variable-level json file that looks like the example at the bottom of this post. What I need to do is search for a particular value, let's say "Gap junction" and return the value for "name" for all of the higher level parents, in this example "Junction", "Plasma membrane" and "Cell". I need to do this using php and I think I need a recursive loop to traverse the array that will record the "name" value for each depth level into an array and then return this "name" array once the search term is found, but I'm struggling a bit at achieving this. Any help would be appreciated.</p>
  
  <pre><code>{
      "name": "Cell",
      "children": [
          {
              "name": "Plasma membrane",
              "children": [
                  {
                      "name": "Junction",
                      "children": [
                          {"name": "Adherens junction"},
                          {"name": "Caveola"},
                          {"name": "Gap junction"},
                          {"name": "Lipid raft"},
                          {"name": "Tight junction"}
                      ]
                  },
                  {"name": "Focal adhesion"}
              ]
          },
          {
              "name": "Vesicle",
              "children": [
                  {
                      "name": "Endosome",
                      "children": [
                          {"name": "Early Endosome"},
                          {"name": "Late Endosome"},
                          {"name": "Recyling Endosome"}
                      ]
                  },
                  { "name": "Microsome"}
              ]
          }
      ]
  }
  </code></pre>
  
  <p><b>EDIT</b></p>
  
  <p>Current code as requested. The $found variable is certainly not working how I intended it too. Code is based/modified on this answer: <a href="https://stackoverflow.com/questions/26751573/get-parent-and-child-depth-level-from-json-using-php">Get Parent and Child depth level from JSON using PHP?</a></p>
  
  <pre><code>$jsonString = file_get_contents("./information/localization.json");
  $jsonArray = json_decode($jsonString);
  
  $currOrganelle = "Gap junction";
  
  $parents = read_tree_recursively($jsonArray, $currOrganelle);
  
  function read_tree_recursively($items, $searchTerm, $result = array(), $level = 0, $found = false) {
  
      foreach($items as $child) {
          if(!$found) {
              $currName = $child-&gt;name;
  
              if($currName == $searchTerm) {
                  $found = true;
                  return $result;
              }
              elseif(!empty($child-&gt;children)) {
                  $result[$level] = $currName;
                  $result = read_tree_recursively($child-&gt;children, $searchTerm, $result, $level + 1, $found);
                  if($found) return $result;
              }
              else {
              }
          }
          else {
              return $result;
          }
      }
  }
  </code></pre>