title: String to Multidimensional Array with merging and sorting
body: <p>I have this sort of string that come from my database after many use of CONCAT().</p>
  
  <p><code>1, AA, 6, 10; 1, Z, 1, 5; 2, AE, 1, 5; 2, AF, 6, 10; 3, X, 1, 5; 3, Y, 5, 10</code></p>
  
  <p>In order to have something clear, let's say this :</p>
  
  <ul>
  <li>The large string is made of multiple substring, all ending by a <code>;</code></li>
  <li>Each substring is made of four elements :
  
  <ul>
  <li>A first number. Let's call it the <code>Group</code></li>
  <li>A letter or group of letters. They are called the <code>Section</code></li>
  <li>A second number. We will call it <code>From</code></li>
  <li>A third and last number. We will call it <code>To</code></li>
  </ul></li>
  </ul>
  
  <p>Using this code</p>
  
  <pre><code>$data = "1, AA, 6, 10; 1, Z, 1, 5; 2, AE, 1, 5; 2, AF, 6, 10; 3, X, 1, 5; 3, Y, 5, 10";
  echo $data;
      $ret = array_map (
      function ($_) {return explode (', ', $_);},
          explode (';', $data)
      );
  </code></pre>
  
  <p>I get the following multidimensional array</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [0] =&gt; 1 
              [1] =&gt; AA 
              [2] =&gt; 6 
              [3] =&gt; 10 
          )
  
      [1] =&gt; Array 
          ( 
              [0] =&gt; 1 
              [1] =&gt; Z 
              [2] =&gt; 1 
              [3] =&gt; 5 
          ) 
  
      [2] =&gt; Array 
          ( 
              [0] =&gt; 2 
              [1] =&gt; AE 
              [2] =&gt; 1 
              [3] =&gt; 5 
          ) 
  
      [3] =&gt; Array 
          ( 
              [0] =&gt; 2 
              [1] =&gt; AF 
              [2] =&gt; 6 
              [3] =&gt; 10 
          ) 
  
      [4] =&gt; Array 
          ( 
              [0] =&gt; 3 
              [1] =&gt; X 
              [2] =&gt; 1 
              [3] =&gt; 5 
          ) 
  
      [5] =&gt; Array 
          ( 
              [0] =&gt; 3 
              [1] =&gt; Y 
              [2] =&gt; 5 
              [3] =&gt; 10 
          ) 
  
  )
  </code></pre>
  
  <p>I want to end with a multidimensional array that looks like that : </p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [0] =&gt; 1 
              [1] =&gt; Array
                  (                   
                      [0] =&gt; Z
                      [1] =&gt; 1 
                      [2] =&gt; 5 
                  )
              [2] =&gt; Array
                  (                   
                      [0] =&gt; AA 
                      [1] =&gt; 6
                      [2] =&gt; 10
                  )
          )
  
      [1] =&gt; Array 
          ( 
              [0] =&gt; 2 
              [1] =&gt; Array
                  (
                      [1] =&gt; AE 
                      [2] =&gt; 1 
                      [3] =&gt; 5 
                  )
              [2] =&gt; Array
                  (
                      [1] =&gt; AF 
                      [2] =&gt; 6 
                      [3] =&gt; 10
                  )
          ) 
  
      [2] =&gt; Array 
          ( 
              [0] =&gt; 3 
              [1] =&gt; Array
                  (
                      [1] =&gt; X 
                      [2] =&gt; 1 
                      [3] =&gt; 5 
                  )
              [2] =&gt; Array
                  (
                      [1] =&gt; Y 
                      [2] =&gt; 6 
                      [3] =&gt; 10
                  )
          )
  ) 
  </code></pre>
  
  <p>I don't know what's the best method to do it in order to get this result nor if it's possible without too complex operations.
  What's the best approach to get the final result ? Should I work on the "raw" multidimensional array or should I work from the string from the beginning ?</p>
  
  <p><strong>Merging arrays with same <code>Group</code></strong></p>
  
  <p>First, I want to merge substring starting with the same <code>Group</code> value. The rest of each string then become another array inside the newly created array. 
  I think I can do this by parsing the initial array (Or maybe the string itself ?) and pushing the results in a new one, merging the data in the same time.
  In my example, it has been done to the six starting arrays that are now grouped into three arrays.</p>
  
  <p><strong>Sorting using the <code>From</code> and <code>To</code> values</strong></p>
  
  <p>After my substrings with the same <code>Group</code> value are combined into arrays, I want to sort these arrays so the <code>To</code> value of a sub-array is always smaller to the <code>From</code> value of the next one. 
  The initial string are always generated with the following rules :</p>
  
  <ul>
  <li>There will never be overlapping <code>From</code> and <code>To</code> values like that : <code>1,5 - 4,7</code>. It would be either <code>1,4 - 5,7</code> or <code>1,5 - 6,7</code></li>
  <li>There will never be missing number in a sequence so you will never have something like <code>1,3 - 5,7</code></li>
  <li>The <code>From</code> and <code>To</code> value of the same array can be the same. So you can get : <code>1,4 - 5,5 - 6,7</code> </li>
  </ul>
  
  <p>That's why <code>1, AA, 6, 10; 1, Z, 1, 5;</code> has been sorted to  <code>Z, 1, 5</code> followed <code>AA, 6, 10</code>. Is there a PHP function that can do this kind of sorting (By comparing values with a different key) or should I create my own ?</p>
  
  <p>Also, if you think that I should totally change the way I want to do this (Using multidimensional arrays...) and you have some ideas, go ahead and share them. I'm also here to learn new way to do things.</p>
  
  <p><strong>Edition concerning Database structure</strong></p>
  
  <p>As stated by @deceze, it may be easier to construct my initial string differently during my SQL query. I have tried it but I'm probably not skilled enough to get what I want. My database looks like that :
  <a href="http://i.stack.imgur.com/9o0as.png" rel="nofollow">Database Structure</a>
  As you can see, all the rows are linked by a MasterID. This is because I'm doing a 1 to n query on multiple table.
  So far, my query looks like that (I removed all the unnecessary stuff from it) : </p>
  
  <pre><code>SELECT s.MasterID,
      GROUP_CONCAT(CONCAT( CONCAT( CONCAT( CONCAT( CONCAT(
          s2d.Group, ', '),
          s2d.Section, ', '),
          s2d.From),', '),
          s2d.To) SEPARATOR '; ') AS Concatstuff,
      FROM table1 AS s
      JOIN table2 AS s2d
          ON s2d.MasterID = s.MasterID
      WHERE MasterID = $MasterID (A PHP variable)
  </code></pre>
  
  <p>Because of the 1 to N relation, I don't know how to make this differently. Maybe I could keep it simpler and perform two queries instead of one. In the first query, I get the information from table1 and in the second query, the informations from table2.
  But if you know a way to do all of that in only one query, I'm eager to learn how you can do this !</p>