title: PHP - Reorganize Query String Parameters
body: <p>Suppose I have a query string like this:</p>
  
  <pre><code>?foo1bar1=a&amp;foo1bar2=b&amp;foo1bar3=c&amp;foo2bar1=d&amp;cats1dogs1=z
  </code></pre>
  
  <p>The parameters in this string could be arbitrary, and could have any number of indexes (so you could have just <code>foo=</code>, you could have <code>foo1bar1=</code> or something like <code>foo1bar1baz1=</code>. However, the parameters and their relevant indexes will be known ahead of time.</p>
  
  <p>I'd like to be able to take this query string, plus a configuration, and re-structure it... The configuration might look something like this:</p>
  
  <pre><code>$indexes = array('foodex', 'bardex');
  $columns = array('foo&lt;foodex&gt;bar&lt;bardex&gt;', 'cats&lt;foodex&gt;dogs&lt;bardex&gt;');
  </code></pre>
  
  <p>And the desired output would be the "columns" reorganized into rows indexed by the appropriate indexes, ready for storing in database rows. Something like this...</p>
  
  <pre><code>array(
      array(
          'foodex' =&gt; 1,
          'bardex' =&gt; 1,
          'foo&lt;foodex&gt;bar&lt;bardex&gt;' =&gt; 'a',
          'cats&lt;foodex&gt;dogs&lt;bardex&gt;' =&gt; 'z'
      ),
      array(
          'foodex' =&gt; 1,
          'bardex' =&gt; 2,
          'foo&lt;foodex&gt;bar&lt;bardex&gt;' =&gt; 'b',
          'cats&lt;foodex&gt;dogs&lt;bardex&gt;' =&gt; null
      ),
      etc.
  )
  </code></pre>
  
  <p>I've thought of a couple ideas for solving this problem, but nothing seems terribly elegant... I could:</p>
  
  <ol>
  <li>Write a recursive function that loops through all possible values of a known index, and then calls itself to loop through all possible values of the next known index, then records the results. This would be super slow... you might loop through thousands or millions of possible index values only to find a handful in the query string.</li>
  <li>Loop through each actual value in the query string, do some sort of regex check to see if it matches one of the columns I'm looking for including wildcards for each index that's listed within it. Then I could build some sort of multi-dimensional array using the indexes and eventually flatten it for the output. This would run much faster, but seems awfully complex.</li>
  </ol>
  
  <p>Is there an elegant solution staring me in the face? I'd love to hear suggestions.</p>