Advertisement
Achilles

Sorting mutidimensional array

Jul 11th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. <?php
  2.  
  3. // function cmp( $a , $b )
  4. // {
  5. // if( $a == $b )
  6. // {
  7. // return 0;
  8. // }
  9.  
  10. // return ($a > $b) ? 1 : -1;
  11. // }
  12.  
  13. function name_sort( $x , $y )
  14. {
  15. return strcmp($x['name'], $y['name']);
  16. }
  17.  
  18. function grade_sort($x , $y )
  19. {
  20. return $x['grade'] <
  21. $y['grade'];
  22. }
  23.  
  24. $students = array (
  25.  
  26. 256 => array ('name' => 'Jon', 'grade' => 98.5),
  27. 2 => array ('name' => 'Vance', 'grade' => 85.1),
  28. 9 => array ('name' => 'Stephen', 'grade' => 94.0),
  29. 364 => array ('name' => 'Steve', 'grade' => 85.1),
  30. 68 => array ('name' => 'Rob', 'grade' => 85.1)
  31.  
  32. );
  33.  
  34.  
  35. echo '<h3>Array As is</h3><pre>' . print_r($students , 1) . '</pre>';
  36.  
  37. uasort($students, 'name_sort');
  38.  
  39. echo '<h3>Array Sorted By Name</h3><pre>' . print_r($students , 1) . '</pre>';
  40.  
  41. uasort($students, 'grade_sort');
  42.  
  43. echo '<h3>Array sorted by grade</h3><pre>' . print_r($students , 1) . '</pre>';
  44.  
  45.  
  46.  
  47.  
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement