title: How to sort a multidimensional array by multiple columns?
body: <p>I'm trying to do the same as MySQL query</p>
  
  <p><code>SELECT * FROM table ORDER BY field1, field2, ...</code></p>
  
  <p>but with php and a multidimensional array:</p>
  
  <pre><code>$Test = array(
      array("a"=&gt;"004", "n"=&gt;"03"),
      array("a"=&gt;"003", "n"=&gt;"02"),
      array("a"=&gt;"001", "n"=&gt;"02"),
      array("a"=&gt;"005", "n"=&gt;"01"),
      array("a"=&gt;"001", "n"=&gt;"01"),
      array("a"=&gt;"004", "n"=&gt;"02"),
      array("a"=&gt;"003", "n"=&gt;"01"),
      array("a"=&gt;"004", "n"=&gt;"01")
  );
  function msort(&amp;$array, $keys){
      array_reverse($keys);
      foreach($keys as $key){
          uasort($array, sortByKey);
      }
      //
      function sortByKey($A, $B){
  
          global $key;
  
          $a = $A[$key];
          $b = $B[$key];
          if($a==$b) return 0;
          return ($a &lt; $b)? -1 : 1 ;
      }
  }
  //
  msort($Test, array("a","n"));
  //
  foreach($Test as $t){
      echo('&lt;p&gt;'.$t["a"].'-'.$t["n"].'&lt;/p&gt;');
  }
  </code></pre>
  
  <p>My theory is: if I sort multiple times on columns with "lesser importance" then columns of "greater importance", I'll achieve an order like the above MySQL query.</p>
  
  <p>Unfortunately, php is returning:</p>
  
  <blockquote>
    <p>Warning: uasort() expects parameter 2 to be a valid callback, function 'sortByKey' not found or invalid function name in /Library/WebServer/Documents/www/teste.array_sort.php on line 23" (uasort line)</p>
  </blockquote>
  
  <p>It's a simple order function. What am I missing?</p>