title: PHP Recursive menu function
body: <p>I have a very similar situation to this post </p>
  
  <p><a href="https://stackoverflow.com/questions/18163449/php-nested-menu-with-a-recursive-function-expand-only-some-nodes-not-all-the/19597804#19597804">PHP: nested menu with a recursive function, expand only some nodes (not all the tree)</a></p>
  
  <p>and I need some help...</p>
  
  <p>Here is the (partial) contents of my $menuJSONArray variable (used when calling the function): </p>
  
  <pre><code>    Array
  (
  [0] =&gt; Array
      (
          [Menu_IDX] =&gt; 1
          [Order] =&gt; 1
          [Name] =&gt; History
          [Parent] =&gt; 
          [Path] =&gt; History
          [Link] =&gt; 
      )
  
  [1] =&gt; Array
      (
          [Menu_IDX] =&gt; 2
          [Order] =&gt; 25
          [Name] =&gt; Review
          [Parent] =&gt; 
          [Path] =&gt; Review
          [Link] =&gt; Review
      )
  
  [2] =&gt; Array
      (
          [Menu_IDX] =&gt; 3
          [Order] =&gt; 35
          [Name] =&gt; Past Medical History
          [Parent] =&gt; 
          [Path] =&gt; Past Medical History
          [Link] =&gt; Past Medical History
      )
   [3] =&gt; Array
      (
          [Menu_IDX] =&gt; 4
          [Order] =&gt; 45
          [Name] =&gt; Item 1
          [Parent] =&gt; 0
          [Path] =&gt; Item 1
          [Link] =&gt; Item 1
      )
   [4] =&gt; Array
      (
          [Menu_IDX] =&gt; 5
          [Order] =&gt; 55
          [Name] =&gt; Item 2
          [Parent] =&gt; 0
          [Path] =&gt; Item 2
          [Link] =&gt; Item 2
      )
   [5] =&gt; Array
      (
          [Menu_IDX] =&gt; 6
          [Order] =&gt; 65
          [Name] =&gt; Item 3
          [Parent] =&gt; 0
          [Path] =&gt; Item 3
          [Link] =&gt; Item 3
      )
  )
  </code></pre>
  
  <p>and so on...</p>
  
  <p>I am using the function below but I keep getting stuck in the loop at the first item. Really need some help wrapping my brain around this function. Thank you all ahead of time, this community rocks!!</p>
  
  <pre><code>///RECURSIVE MENU
  function recursive($parent, $array) {
  $has_children = false; //set value of has children to false by default
  foreach($array as $key =&gt; $value) {  //loop through the array as key-value pairs
      if ($value['Parent'] == $parent) {  //if the value of Parent field is equal to parent variable   
          if ($has_children === false &amp;&amp; $parent) {  //if children is false but parent is not null, this is a sub item
              $has_children = true;  //children is true
              echo '&lt;ul&gt;' ."\n";  //create parent menu ul
          } //otherwise just create the item
          echo '&lt;li&gt;' . "\n";  //create li for parent menu item
          echo '&lt;a href="'.$value['Path'].'"&gt;' . $value['Name'] . '&lt;/a&gt;' . " \n";  //create link for menu item
              recursive($key, $array); //create sub menu
          echo "&lt;/li&gt;\n"; //end parent menu item
      }
  }
  if ($has_children === true &amp;&amp; $parent) echo "&lt;/ul&gt;\n"; //end parent menu
  }
  
  ?&gt;
  &lt;?php echo recursive(0, $menuJSONArray); ?&gt;&lt;/ul&gt;
  </code></pre>
  
  <p>What I get in return is:</p>
  
  <pre><code>&lt;ul&gt;
  
  
  
  &lt;li&gt;
  &lt;a href="History"&gt;History&lt;/a&gt; 
  &lt;li&gt;
  &lt;a href="History"&gt;History&lt;/a&gt; 
  &lt;li&gt;
  &lt;a href="History"&gt;History&lt;/a&gt; 
  &lt;li&gt;
  &lt;a href="History"&gt;History&lt;/a&gt; 
  &lt;li&gt;
  &lt;a href="History"&gt;History&lt;/a&gt; 
  &lt;li&gt;
  &lt;a href="History"&gt;History&lt;/a&gt;
  </code></pre>
  
  <p>Can't seem to get out of this loop. Thanks!!</p>
  
  <p>P.S. I am not concerned with the collapsing of the tree etc. from the referenced post as I will be handling that with jquery and css. I just cannot get the proper output of the  menu syntax.</p>