title: Order Associative Array by Specific Key Values
body: <p>I have the following array:</p>
  
  <pre><code>   $data=array(
     'Points'=&gt;$points,'Name'=&gt;$row['Name'], 'Phone'=&gt;$row['phone']
      );
  </code></pre>
  
  <p>This resides in a for each loop in my Codeigniter controller index function:</p>
  
  <pre><code>public function index(){
  
      $query=$this-&gt;My_model-&gt;get_data();
      foreach ($query as $row)
      {
             $data=array(
             'Points'=&gt;$points,'Name'=&gt;$row['Name'], 'Phone'=&gt;$row['phone']
              );
  
      }
  }
  </code></pre>
  
  <p>Currently if I print_r on $data it would produce:</p>
  
  <pre><code>Array ( [Points] =&gt; 500 [Name] =&gt; Dave Laus ) 
  Array ( [Points] =&gt; 1200 [Name] =&gt; John Smith ) 
  Array ( [Points] =&gt; 700 [Name] =&gt; Jason Smithsonian ) 
  </code></pre>
  
  <p>However I would like to sort/order this so that the user with the highest points showing first like this:</p>
  
  <pre><code>Array ( [Points] =&gt; 1200 [Name] =&gt; John Smith ) 
  Array ( [Points] =&gt; 700 [Name] =&gt; Jason Smithsonian ) 
  Array ( [Points] =&gt; 500 [Name] =&gt; Dave Laus ) 
  </code></pre>
  
  <p>I want to sort the array by the "Points" key, so that the user with the highest points appear first. I want to re order the array to show from highest to lowest points.</p>
  
  <p>I have tried usort and arsort and ksort. I haven't gotten it to work.</p>
  
  <p>How do I do this?</p>
  
  <p>I tried this in my controller, but it's doesn't work, errors instead:</p>
  
  <pre><code>public function index(){
  
      $query=$this-&gt;My_model-&gt;get_data();
      foreach ($query as $row)
      {
             $data=array(
             array('Points'=&gt;$points,'Name'=&gt;$row['Name'], 'Phone'=&gt;$row['phone']),
              );
  
             function cmp ($a, $b) {
          return $a['Points'] &lt; $b['Points'] ? 1 : -1;
          }
  
          usort($data, "cmp");
  
              print_r($data);
  
  
                  //I also tried usort($leaders, array('home', 'cmp')); whcih gave no errors, but was the same result as before, not ordered
  
  
          }
      }
  </code></pre>