title: Create a hierarchical list of array elements
body: <p>I have a an array of products. Each product contains its category and subcategories listed in a hierarchical order:</p>
  
  <pre>
  Array
  (
      [product_id_1] => Array
          (
              [0] => Men
              [1] => Sunglasses
              [2] => Luxury
              [3] => Ray-ban
          )
  
      [product_id_2] => Array
          (
              [0] => Women
              [1] => Lenses
              [2] => Casual
              [3] => Gucci
          )
      [product_id_3] => Array
          (
              [0] => Men
              [1] => Sunglasses
              [2] => Casual
              [3] => Prada
          )
  [...]
  )
  
  </pre>
  
  <p>I want to create an unordered hierarchical HTML menu like so:</p>
  
  <pre>
  -Men
  --Sunglasses
  ---Luxury
  ----Ray-ban
  ---Casual
  ----Prada
  -Women
  --Lenses
  ---Casual
  ----Gucci
  </pre>
  
  <p>The function should strip out repetitive categories and subcategories. This script returns the array of products that I've posted at the top:</p>
  
  <p><code></p>
  
  <pre><code>&lt;?php
      function displayNestedMenu( $posts, $taxonomies ) {
          foreach ( $posts as $post ) {
              foreach ( $taxonomies as $key =&gt; $taxonomy ) {
                  $push = wp_get_object_terms( $post-&gt;ID, $taxonomy );
                  if ( !empty( $push ) ) {
                          $list[$post-&gt;ID][] = $push[0]-&gt;name;
                  }
              }
          }
          return $list;
      }
      print_r( displayNestedMenu( $posts, $taxonomies ) );
  ?&gt;
  </code></pre>
  
  <p></code></p>
  
  <p>I imagine the solution should invoke the function inside the function but after trying a couple of methods I haven't succeeded yet. Any suggestions are appreciated!</p>