title: Trying to sort an associative array in PHP
body: <p>I'm trying to sort this associative array in PHP, sorting by field <code>name</code></p>
  
  <pre><code>$fonts = array(
      0 =&gt; ["index" =&gt; 0, "name" =&gt; "Arial", "path" =&gt; "assets/fonts/arial.ttf"],
      1 =&gt; ["index" =&gt; 1, "name" =&gt; "Times", "path" =&gt; "assets/fonts/times.ttf"],
      2 =&gt; ["index" =&gt; 2, "name" =&gt; "Roboto", "path" =&gt; "assets/fonts/roboto.ttf"],
      3 =&gt; ["index" =&gt; 3, "name" =&gt; "AlexBrush", "path" =&gt; "assets/fonts/AlexBrush-Regular.ttf"],
  );
  </code></pre>
  
  <p>What I'm trying to do is to sort the entries by name but keep the keys associated with the entry itself, so what I want is:</p>
  
  <pre><code> $fonts = array(
      3 =&gt; ["index" =&gt; 3, "name" =&gt; "AlexBrush", "path" =&gt; "assets/fonts/AlexBrush-Regular.ttf"],
      0 =&gt; ["index" =&gt; 0, "name" =&gt; "Arial", "path" =&gt; "assets/fonts/arial.ttf"],
      2 =&gt; ["index" =&gt; 2, "name" =&gt; "Roboto", "path" =&gt; "assets/fonts/roboto.ttf"],
      1 =&gt; ["index" =&gt; 1, "name" =&gt; "Times", "path" =&gt; "assets/fonts/times.ttf"],
  );
  </code></pre>
  
  <p>Here's the bare minimum code which exposes the problem (PHP ver +7):</p>
  
  <pre><code>function cmp($a, $b)
  {
      return strcmp($a["name"], $b["name"]);
  }
  
  $fonts = array(
      0 =&gt; ["index" =&gt; 0, "name" =&gt; "Arial", "path" =&gt; "assets/fonts/arial.ttf"],
      1 =&gt; ["index" =&gt; 1, "name" =&gt; "Times", "path" =&gt; "assets/fonts/times.ttf"],
      2 =&gt; ["index" =&gt; 2, "name" =&gt; "Roboto", "path" =&gt; "assets/fonts/roboto.ttf"],
      3 =&gt; ["index" =&gt; 3, "name" =&gt; "AlexBrush", "path" =&gt; "assets/fonts/AlexBrush-Regular.ttf"],
  );
  
  uasort($fonts, "cmp");
  
  for($i = 0; $i &lt; 4; $i++)
  {
      echo "&lt;br/&gt;".$fonts[$i]["name"];
  }
  </code></pre>
  
  <p>I'm using <code>uasort()</code> as it's supposed to keep the keys associated while sorting the array. Unfortunately if I print the array I get the same order, that is:</p>
  
  <pre><code>Arial
  Times
  Roboto
  AlexBrush
  </code></pre>