code_junkie

How do I sort a multidimensional array in php

Nov 11th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. function f_parse_csv($file, $longest, $delimiter)
  2. {
  3. $mdarray = array();
  4. $file = fopen($file, "r");
  5. while ($line = fgetcsv($file, $longest, $delimiter))
  6. {
  7. array_push($mdarray, $line);
  8. }
  9. fclose($file);
  10. return $mdarray;
  11. }
  12.  
  13. foreach ($mdarray as $key => $row) {
  14. $dates[$key] = $row[0];
  15. // of course, replace 0 with whatever is the date field's index
  16. }
  17.  
  18. array_multisort($dates, SORT_DESC, $mdarray);
  19.  
  20. class TableSorter {
  21. protected $column;
  22. function __construct($column) {
  23. $this->column = $column;
  24. }
  25. function sort($table) {
  26. usort($table, array($this, 'compare'));
  27. return $table;
  28. }
  29. function compare($a, $b) {
  30. if ($a[$this->column] == $b[$this->column]) {
  31. return 0;
  32. }
  33. return ($a[$this->column] < $b[$this->column]) ? -1 : 1;
  34. }
  35. }
  36.  
  37. $sorter = new TableSorter(0); // sort by first column
  38. $mdarray = $sorter->sort($mdarray);
  39.  
  40. function sort_multi_array ($array, $key)
  41. {
  42. $keys = array();
  43. for ($i=1;$i<func_num_args();$i++) {
  44. $keys[$i-1] = func_get_arg($i);
  45. }
  46.  
  47. // create a custom search function to pass to usort
  48. $func = function ($a, $b) use ($keys) {
  49. for ($i=0;$i<count($keys);$i++) {
  50. if ($a[$keys[$i]] != $b[$keys[$i]]) {
  51. return ($a[$keys[$i]] < $b[$keys[$i]]) ? -1 : 1;
  52. }
  53. }
  54. return 0;
  55. };
  56.  
  57. usort($array, $func);
  58.  
  59. return $array;
  60. }
  61.  
  62. // a sorter class
  63. // php4 and php5 compatible
  64. class Sorter {
  65.  
  66. var $sort_fields;
  67. var $backwards = false;
  68. var $numeric = false;
  69.  
  70. function sort() {
  71. $args = func_get_args();
  72. $array = $args[0];
  73. if (!$array) return array();
  74. $this->sort_fields = array_slice($args, 1);
  75. if (!$this->sort_fields) return $array();
  76.  
  77. if ($this->numeric) {
  78. usort($array, array($this, 'numericCompare'));
  79. } else {
  80. usort($array, array($this, 'stringCompare'));
  81. }
  82. return $array;
  83. }
  84.  
  85. function numericCompare($a, $b) {
  86. foreach($this->sort_fields as $sort_field) {
  87. if ($a[$sort_field] == $b[$sort_field]) {
  88. continue;
  89. }
  90. return ($a[$sort_field] < $b[$sort_field]) ? ($this->backwards ? 1 : -1) : ($this->backwards ? -1 : 1);
  91. }
  92. return 0;
  93. }
  94.  
  95. function stringCompare($a, $b) {
  96. foreach($this->sort_fields as $sort_field) {
  97. $cmp_result = strcasecmp($a[$sort_field], $b[$sort_field]);
  98. if ($cmp_result == 0) continue;
  99.  
  100. return ($this->backwards ? -$cmp_result : $cmp_result);
  101. }
  102. return 0;
  103. }
  104. }
  105.  
  106. /////////////////////
  107. // usage examples
  108.  
  109. // some starting data
  110. $start_data = array(
  111. array('first_name' => 'John', 'last_name' => 'Smith', 'age' => 10),
  112. array('first_name' => 'Joe', 'last_name' => 'Smith', 'age' => 11),
  113. array('first_name' => 'Jake', 'last_name' => 'Xample', 'age' => 9),
  114. );
  115.  
  116. // sort by last_name, then first_name
  117. $sorter = new Sorter();
  118. print_r($sorter->sort($start_data, 'last_name', 'first_name'));
  119.  
  120. // sort by first_name, then last_name
  121. $sorter = new Sorter();
  122. print_r($sorter->sort($start_data, 'first_name', 'last_name'));
  123.  
  124. // sort by last_name, then first_name (backwards)
  125. $sorter = new Sorter();
  126. $sorter->backwards = true;
  127. print_r($sorter->sort($start_data, 'last_name', 'first_name'));
  128.  
  129. // sort numerically by age
  130. $sorter = new Sorter();
  131. $sorter->numeric = true;
  132. print_r($sorter->sort($start_data, 'age'));
  133.  
  134. function sort2d_bycolumn($array, $column, $method, $has_header)
  135. {
  136. if ($has_header) $header = array_shift($array);
  137. foreach ($array as $key => $row) {
  138. $narray[$key] = $row[$column];
  139. }
  140. array_multisort($narray, $method, $array);
  141. if ($has_header) array_unshift($array, $header);
  142. return $array;
  143. }
  144.  
  145. <?php
  146. //sort multidimensional array by $order_in_category
  147. if (count ($in_cat_team_arr) > 0) {
  148. foreach ($in_cat_team_arr as $key => $row) {
  149. $order_in_category[$key] = $row[0];//[0] is $order_in_category
  150. }
  151. array_multisort($order_in_category, SORT_ASC, $in_cat_team_arr);
  152. }
  153.  
  154. foreach ($in_cat_team_arr as $team_mem) {
  155. //loop through the sorted array here
  156. }
Add Comment
Please, Sign In to add comment