title: In php how does usort() function works
body: <p>I have looked at the php documentation, tutorials online and none of them how usort is actually working. I have an example i was playing with below.</p>
  
  <pre><code>$data = array(
  
      array('msg' =&gt; 'some text','month' =&gt; 11,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 5,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 8,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 12,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 2,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 3,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 4,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 7,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 10,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 1,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 6,'level' =&gt; 10),
  
      array('msg' =&gt; 'some text','month' =&gt; 9,'level' =&gt; 10)
  
  );
  </code></pre>
  
  <p>I wanted to be able to sort the months from 12 to 1 (since their unorganized)
  through some help this was the solution</p>
  
  <pre><code>function cmp($a, $b)
  {
      if ($a["month"] == $b["month"]) 
      {
         return 0;
      }
      return ($a["month"] &lt; $b["month"]) ? -1 : 1;
  }
  
  usort($data, "cmp");
  </code></pre>
  
  <p>but i dont understand how the function cmp sorts the array. i tried printing out each variable $a and $b like this:</p>
  
  <pre><code>function cmp($a, $b)
  {
     echo "a: ".$a['month']."&lt;br/&gt;";
     echo " b: ".$b['month']."&lt;br/&gt;";
     echo "&lt;br/&gt;&lt;br/&gt;";
  }
  </code></pre>
  
  <p>and the output was </p>
  
  <pre><code>a: 3
  b: 5
  
  a: 9
  b: 3
  
  a: 3
  b: 8
  
  a: 6
  b: 3
  
  a: 3
  b: 12
  
  a: 1
  b: 3
  
  a: 3
  b: 2
  
  a: 10
  b: 3
  
  a: 3
  b: 11
  
  a: 7
  b: 3
  
  a: 4
  b: 3
  
  a: 12
  b: 2
  
  a: 5
  b: 12
  
  a: 12
  b: 11
  
  a: 8
  b: 12
  
  a: 5
  b: 8
  
  a: 2
  b: 11
  
  a: 6
  b: 9
  
  a: 7
  b: 6
  
  a: 6
  b: 4
  
  a: 10
  b: 6
  
  a: 1
  b: 6
  
  a: 9
  b: 4
  
  a: 7
  b: 1
  
  a: 10
  b: 7
  </code></pre>
  
  <p>it makes no sense to how the sort is working and why cmp($a, $b) is used. i have tried to print out all its processes as you can see but have not come to any solution to how it all works..</p>
  
  <p>thanks</p>