title: Sort an array by multiple keys in PHP
body: <p>I have a table/array. E.g. in Excel I can sort by column 1 asc, column 2 desc, column 3 asc, etc.</p>
  
  <p>Can I do same in PHP? First with <code>["first_name"] ASC</code>, then <code>["last_name"] DESC</code>, <code>["player_id"] ASC</code> and <code>["user_id"] DESC</code>.</p>
  
  <pre class="lang-php prettyprint-override"><code>array(2) {
      [0]=&gt; array(6) {
          [0]=&gt; string(8) "John",
          ["first_name"]=&gt; string(8) "John",
          [1]=&gt; int(7) "44",
          ["score"]=&gt; int(7) "44",
          [2]=&gt; string(2) "7",
          ["player_id"]=&gt; string(2) "7",
          [3]=&gt; string(2) "3",
          ["user_id"]=&gt; string(2) "3"
      },
      [1]=&gt; array(6) {
          [0]=&gt; string(5) "Sam",
          ["first_name"]=&gt; string(5) "Sam",
          [1]=&gt; int(7) "55",
          ["score"]=&gt; int(7) "55",
          [2]=&gt; string(2) "1",
          ["player_id"]=&gt; string(2) "1",
          [3]=&gt; string(2) "6",
          ["user_id"]=&gt; string(2) "61"
      }
  }
  </code></pre>
  
  <p>(The array is much longer and deeper in reality, this is just an example.)</p>
  
  <p>Update: </p>
  
  <pre class="lang-php prettyprint-override"><code>function byPlayerID($player, $compare) {
      if($player['player_id'] &gt; $compare['player_id'])
          return 1; // move up
      else if($player['player_id'] &lt; $compare['player_id'])
          return -1; // move down
      else
          return 0; // do nothing
  
      if($player['score'] &gt; $compare['score'])
          return 1; // move up
      else if($player['score'] &lt; $compare['score'])
          return -1; // move down
      else
          return 0; // do nothing
  }
  </code></pre>
  
  <p>Update 2: Never mind, I just needed to remove <code>return 0;</code></p>