title: Sort a multidimensional array by Last name in php
body: <p>Is it possible to sort (with php) an array like this by <strong>last name</strong>:</p>
  
  <pre><code>$array = array(
      array("name" =&gt; "Mary Johnson","age" =&gt; 43),
      array("name" =&gt; "Amanda Miller","age" =&gt; 23),
      array("name" =&gt; "James Brown","age" =&gt; 47),
      array("name" =&gt; "Patricia Williams","age" =&gt; 31),
      array("name" =&gt; "Michael Davis","age" =&gt; 15),
      array("name" =&gt; "Sarah Miller","age" =&gt; 35),
      array("name" =&gt; "Patrick Miller","age" =&gt; 44)
    ); 
  </code></pre>
  
  <pre><code>&lt;?php
  // A function to sort by last name.
  function lastNameSort($a, $b) {
      $aLast = end(explode(' ', $a));
      $bLast = end(explode(' ', $b));
  
      return strcasecmp($aLast, $bLast);
  }
  
  
  // The array of data.
  $array = array(
      array("name" =&gt; "Mary Johnson","age" =&gt; 43),
      array("name" =&gt; "Amanda Miller","age" =&gt; 23),
      array("name" =&gt; "James Brown","age" =&gt; 47),
      array("name" =&gt; "Patricia Williams","age" =&gt; 31),
      array("name" =&gt; "Michael Davis","age" =&gt; 15),
      array("name" =&gt; "Sarah Miller","age" =&gt; 35),
      array("name" =&gt; "Patrick Miller","age" =&gt; 44)
    );
  
  // Perform the sort:
  uasort($array, 'lastNameSort');
  
  // Print the result:
  print_r($array);
  </code></pre>