title: A better way to write a multi-dimensional array php
body: <p>I am looking for a better understanding on how I can stack arrays more efficiently possibly using a <code>while</code> loop?</p>
  
  <p>I have this old array that I need to iterate through, explode each, and create a multi-dimensional array. Is there a better way to do this programatically?</p>
  
  <pre><code>$cat[] = "Apparel, accessories &amp; footwear"; 
  $cat[] = "Apparel, accessories &amp; footwear/Men&amp;#39;s"; 
  $cat[] = "Apparel, accessories &amp; footwear/Men&amp;#39;s/Apparel"; 
  $cat[] = "Apparel, accessories &amp; footwear/Men&amp;#39;s/Footwear"; 
  $cat[] = "Apparel, accessories &amp; footwear/Men&amp;#39;s/Accessories";
  
  foreach($cat as $cs){
      $ex = explode("/",$cs);
      $count = count($ex);
      if($count == 1){
          $category[$ex[0]] = array();
      }
      if($count == 2){
          $category[$ex[0]][$ex[1]] = array();
      }
      if($count == 3){
          $category[$ex[0]][$ex[1]][$ex[2]] = array();
      }
      if($count == 4){
          $category[$ex[0]][$ex[1]][$ex[2]][$ex[3]] = array();
      }
      if($count == 5){
          $category[$ex[0]][$ex[1]][$ex[2]][$ex[3]][$ex[4]] = array();
      }
  }
  </code></pre>
  
  <p>The issue is that even though there are only a max of 3 array pieces per iteration, this could be more but I don't want to manually create <code>if($count == x…</code> I need this to be dynamic. There must be a better way?</p>