title: Making a custom php "explode" function recursive
body: <p>I know <a href="http://www.php.net/manual/en/function.http-build-query.php" rel="nofollow">http_build_query()</a> and <a href="http://php.net/manual/en/function.parse-str.php" rel="nofollow">parse_str()</a> are a better solution for this sort of problem but I can't change the structure of the data in the particular project I'm working on so I'm trying to simplify things a little by creating large 3d associative arrays from a database stored string. </p>
  
  <p>Can someone please give me a hand making this function recursive? I want it to take something like this:</p>
  
  <pre><code>explode3D( $separators = array('|',','),
  $string = "val1,val2|val1,val2,val3",
  $keys = "cat1,item1,item2|cat2,item1,item2,item3"
  );
  </code></pre>
  
  <p>And return something like this</p>
  
  <pre><code>Array (
  'cat1' =&gt; Array('item1'=&gt;'val1', 'item2'=&gt;'val2'),
  'cat2' =&gt; Array('item1'=&gt;'val1', 'item2'=&gt;'val2', 'item3'=&gt;'val3')
  )
  </code></pre>
  
  <p>Here's the function so far:</p>
  
  <pre><code>function explode3D($separators,$string,$keys=0){
      $res = array();
  
      if(is_array($separators)){//MULTI DIMENSION MODE: (unfinished!!)
  
          //help needed here//
  
      }else{//SINGLE DIMENSION MODE:
          $vals = explode($separators,$string);
          if($keys === 0){ //NO KEYS:
              $res = $vals;
          }else if($keys === 1){//ALTERNATE ROWS ARE KEYS:
              $key = '';
              for($i=0; $i&lt;sizeof($vals); $i++){
                  if ($i++ % 2 == 1 ){ //every second element:
                      $key = $vals[$i]; //save the key
                  }else{
                      $res[$key] = $vals[$i]; //set the saved key = value
                  }
              }
          }else{ //GET KEYS FROM $keys ARRAY:
              if(is_string($keys)){ //explode keys string if necessary (using $separators)
                  $keys = explode3D($separators,$keys);
              }
              for($i=0; $i&lt;sizeof($vals); $i++){
                  $res[$keys[$i]] = $vals[$i];
              }
          }
      }
      return($res);
  }
  </code></pre>