title: PHP - Build multiple-level HTML menu from php array combining like values based on value name
body: <p>I have searched a bit on SO and haven't been able to find this unique use case, but maybe I don't know how to phrase it.  I am looking to use a PHP array (see format below) to build a dynamic HTML menu that duplicates identical string values into sub menus when a match exists.</p>
  
  <p>This may be too complicated but I would like to find out if it is possible and if someone has a code sample.  <strong>I don't mind changing the 'verse' array value structure to make it more mutable</strong> and get this to work (perhaps turning it into array since there are 1:many potential values in it.)</p>
  
  <p>I could not get the HTML below to format quite right for SO but the gist of the request is that all articles with a matching book/chapter string would get placed together and nested.  If there are 2 entries for Psalms 121 (which there are in the below array), then they should be nested together in the same sub-menu.</p>
  
  <p>Desired End Structure (Note that because 2 strings matched Psalms chapter 121, they were combined together:</p>
  
  <pre><code>Hebrews
      Chapter 12
          Verse 26-28
      Chapter 9
          Verse 23-24
  
  Psalms
      Chapter 121
           Verse 3-4
           Verse 7-10
      Chapter 16
           Verse 11
  
  Exodus
      Chapter 33
           Verse 14
  </code></pre>
  
  <p>Thank you in advance for your help!</p>
  
  <p>PHP Array:</p>
  
  <pre><code>&lt;?php
  
  $articles = array(
  
  array(
      'contentid' =&gt; '109',
      'full' =&gt; 'Song by Artist (Ref 109)',
      'verse' =&gt; 'Hebrews 12:26-28'),
  
  array(
      'contentid' =&gt; '110',
      'full' =&gt; 'Song by Artist (Ref 110)',
      'verse' =&gt; 'Psalms 121:3-4'),
  
  array(
      'contentid' =&gt; '111',
      'full' =&gt; 'Song by Artist (Ref 111)',
      'verse' =&gt; 'Hebrews 9:23-24'),
  
  array(
      'contentid' =&gt; '112',
      'full' =&gt; 'Song by Artist (Ref 112)',
      'verse' =&gt; 'Psalms 16:11; Exodus 33:14; Psalms 121:7-10'));
  
  ?&gt;
  </code></pre>
  
  <p>I would like this to draw an HTML menu that looks like this:</p>
  
  <pre><code>&lt;!-- Hebrews --&gt;
                      &lt;div class="submenu"&gt;
                          &lt;a href="#"&gt;**Hebrews**&lt;/a&gt;
                          &lt;!-- Level 2 menu --&gt;
                          &lt;div&gt;
                              &lt;div&gt;
  
  
                                  &lt;div class="submenu"&gt;
                                      &lt;a href="#"&gt;Chapter *12*&lt;/a&gt;
                                      &lt;!-- Level 3 menu --&gt;
                                      &lt;div&gt;
                                          &lt;div&gt;
                                              &lt;a href="#"&gt;&lt;span&gt;Song by Artist (Ref 109)&lt;/span&gt;Verse *26-28*&lt;/a&gt;
                                      &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
                                  &lt;div class="submenu"&gt;
                                      &lt;a href="#"&gt;Chapter *9*&lt;/a&gt;
                                      &lt;!-- Level 3 menu --&gt;
                                      &lt;div&gt;
                                          &lt;div&gt;
                                              &lt;a href="#"&gt;&lt;span&gt;Song by Artist (Ref 111)&lt;/span&gt;Verse *23-24*&lt;/a&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  
  &lt;!-- Psalms --&gt;
  
                      &lt;div class="submenu"&gt;
                          &lt;a href="#"&gt;**Psalms**&lt;/a&gt;
                          &lt;!-- Level 2 menu --&gt;
                          &lt;div&gt;
                              &lt;div&gt;
  
                                  &lt;div class="submenu"&gt;
                                      &lt;a href="#"&gt;Chapter *121*&lt;/a&gt;
                                      &lt;!-- Level 3 menu --&gt;
                                      &lt;div&gt;
                                          &lt;div&gt;
                                              &lt;a href="#"&gt;&lt;span&gt;Song by Artist (Ref 110)&lt;/span&gt;Verse *3-4*&lt;/a&gt;
                                              &lt;a href="#"&gt;&lt;span&gt;Song by Artist (Ref 112)&lt;/span&gt;Verse *7-10*&lt;/a&gt;                                       &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
  </code></pre>
  
  
  
  
          Chapter *9*
  
  
          
                                                                                 
              Song by Artist (Ref 111)Verse *23-24*