title: How to sort a multidimensional array of columns by a specific column in PHP?
body: <p>I found on the PHP documentation the function "array_multisort" that is meant to sort an array "of columns". They provide one example where the user has an array of rows and then the user has to transform this array of rows into an array of columns. In my case the array is already set by columns, such as:</p>
  
  <pre><code>tablearray 
  
  {
  
  ['Employee_ID'] = {0 =&gt; row1, 1 =&gt; row2, 2 =&gt; row3, 3 =&gt; row4}
  
  ['First_Name'] = {0 =&gt; row1, 1 =&gt; row2, 2 =&gt; row3, 3 =&gt; row4}
  
  ['LastName'] = {0 =&gt; row1, 1 =&gt; row2, 2 =&gt; row3, 3 =&gt;row4}
  
   }
  </code></pre>
  
  <p>I want to sort by Employee_ID and I need all the other columns to follow the same order. I tried:</p>
  
  <pre><code>   array_multisort($tablearray['Employee_ID'], SORT_ASC);
  </code></pre>
  
  <p>But it only sorts the first column (which becomes a mess). The array has more than 10 columns and it changes the column names depending on the search (the columns names are its keys). 
  On PHP's documentation for this function, the example provided shows that the after transforming the rows array into a columns array, we should use the original array as a third parameter to match the keys - I don't have the "original" array to do the match since I didn't transform anything. </p>
  
  <p>Thank you.</p>
  
  <hr>
  
  <p>Desired output, as suggested by one user:</p>
  
  <pre><code>Original:
  
  array
  {
  
  ['Employee_ID'] = (1002, 4508, 0002, 1112)
  ['Business_Unit'] = ('UER', 'ABC', 'XYZ', 'EER')
  ['LastName'] = ('Smith', 'Vicente', 'Simpson', 'Thompson')
  
  }
  
  Sorted by Employee ID:
  
  array
  {
  
  ['Employee_ID'] = (0002, 1002, 1112, 4508)
  ['Business_Unit'] = ('XYZ', 'UER', 'EER', 'ABC')
  ['LastName'] = ('Simpson','Smith', 'Thompson', 'Vicente')
  
  }
  </code></pre>
  
  <p>--
  My original array is a database query output:</p>
  
  <pre><code>Array
   (
  [0] =&gt; Array
      (
          [Employee_ID] =&gt; 0000
          [Supervisor_ID] =&gt; 00000
          [Location_Descr] =&gt; somewhere
          [Start_Date] =&gt; 06/03/2002
          [Service_Date] =&gt; 06/03/2002
          [Rehire_Date] =&gt; 00/00/00
          [Business_Unit] =&gt; YYYY
          [Job_Title] =&gt; Manager
          [Email] =&gt; email@example.com
          [Dept_Desc] =&gt; bla bla bla
          [Employee_Name_LF] =&gt; Last, First
          [Supervisor_Name_LF] =&gt; Last, First
          [Term_Date] =&gt; 00/00/00
          [Preferred_Name] =&gt; Someone
          [Source] =&gt; Sheet2
      )
  )
  </code></pre>
  
  <p>There a several more rows. 
  The main purpose is to show the results as an HTML table and to generate a CSV file. I already made those functions using the modified structure (the first that I posted). I thought it would be easier to deal with that structure... Indeed it was, but not for sorting unfortunately. </p>
  
  <p>The array_multisort documentation (<a href="http://php.net/manual/en/function.array-multisort.php" rel="nofollow">http://php.net/manual/en/function.array-multisort.php</a>) suggests separating each column as an individual array.. However, as you can see I have several columns (and the user can select more or less to be shown before performing the query.. So I can't just list all of them on the statement).</p>
  
  <p>I a willing to change everything just to make the code better to be worked with.</p>