php专用数组排序类ArraySortUtil用法实例

这篇文章主要介绍了php专用数组排序类ArraySortUtil用法,实例分析了ArraySortUtil实现数组排序的方法与对应使用技巧,需要的朋友可以参考下。

本文实例讲述了php专用数组排序类ArraySortUtil用法,分享给大家供大家参考,具体如下:

  1. **
  2. * ArraySortUtil is a array sort utility, you can extends the sorting engine.
  3. *
  4. * @version 0.1
  5. * @package utility.sort
  6. */
  7. class ArraySortUtil
  8. {
  9. static function uasort($unsort, $fields)
  10. {
  11. if ( !is_array($unsort) || sizeof($unsort) <= 0 ) return $unsort;
  12. $sorted = uasortEngine::uasort($unsort, $fields);
  13. return $sorted;
  14. }
  15. static function multisort($unsort, $fields)
  16. {
  17. if ( !is_array($unsort) || sizeof($unsort) <= 0 ) return $unsort;
  18. $sorted = multisortEngine::multisort($unsort, $fields);
  19. return $sorted;
  20. }
  21. }
  22. class multisortEngine
  23. {
  24. static function multisort($unsort, $fields)
  25. {
  26. $sorted = $unsort;
  27. if (is_array($unsort))
  28. {
  29. $loadFields = array();
  30. foreach($fields as $sortfield)
  31. {
  32. $loadFields["field"][] = array(
  33. "name" => $sortfield["field"],
  34. "order" => $sortfield["order"],
  35. "nature" => $sortfield["nature"],
  36. "caseSensitve" => $sortfield["caseSensitve"]
  37. );
  38. $loadFields["data"][$field["field"]] = array();
  39. }
  40. // Obtain a list of columns
  41. foreach ($sorted as $key => $row) {
  42. foreach($loadFields["field"] as $field) {
  43. $value = $row[$field["name"]];
  44. $loadFields["data"][$field["name"]][$key] = $value;
  45. }
  46. }
  47. $parameters = array();
  48. foreach($loadFields["field"] as $sortfield) {
  49. $array_data = $loadFields["data"][$sortfield["name"]];
  50. $caseSensitve = ( $sortfield["caseSensitve"] == null ) ? $sortfield["caseSensitve"] : false;
  51. if (!$caseSensitve) $array_data = array_map('strtolower', $array_data);
  52. $parameters[] = $array_data;
  53. if ( $sortfield["order"] != null ) $parameters[] = ( $sortfield["order"] ) ? SORT_DESC : SORT_ASC;
  54. if ( $sortfield["nature"] != null ) $parameters[] = ( $sortfield["nature"] ) ? SORT_REGULAR : SORT_STRING;
  55. }
  56. $parameters[] = &$sorted;
  57. call_user_func_array("array_multisort", $parameters);
  58. }
  59. return $sorted;
  60. }
  61. }
  62. class uasortEngine
  63. {
  64. static private $caseSensitve = false;
  65. static private $sortfields = array();
  66. static private $sortorder = true;
  67. static private $nature = false;
  68. static private function uasort_callback(&$a, &$b)
  69. {
  70. foreach(self::$sortfields as $sortfield)
  71. {
  72. $_field = $sortfield["field"];
  73. $_order = isset($sortfield["order"]) ? $sortfield["order"] : self::$sortorder;
  74. $_caseSensitve = isset($sortfield["caseSensitve"]) ? $sortfield["caseSensitve"] : self::$caseSensitve;
  75. $_nature = isset($sortfield["nature"]) ? $sortfield["nature"] : self::$nature;
  76. if ($_field != "")
  77. {
  78. $retval = 0;
  79. if ($_nature)
  80. {
  81. if ($_caseSensitve)
  82. {
  83. $compare = strnatcmp($a[$_field], $b[$_field]);
  84. }
  85. else
  86. {
  87. $compare = strnatcasecmp($a[$_field], $b[$_field]);
  88. }
  89. }
  90. else
  91. {
  92. if ($_caseSensitve)
  93. {
  94. $compare = strcmp($a[$_field], $b[$_field]);
  95. }
  96. else
  97. {
  98. $compare = strcasecmp($a[$_field], $b[$_field]);
  99. }
  100. }
  101. if ($compare !== 0 && !$_order) $compare = ($compare > 0) ? -1 : 1;
  102. }
  103. if ($compare !== 0) break;
  104. }
  105. return $compare;
  106. }
  107. static function uasort($unsort, $fields)
  108. {
  109. self::$sortfields = $fields;
  110. $sorted = $unsort;
  111. uasort($sorted, array('uasortEngine', 'uasort_callback'));
  112. return $sorted;
  113. }
  114. }

使用范例如下:

  1. <?php
  2. require_once('class.ArraySortUtil.php');
  3. // declare test data
  4. $assetData[] = array("id" => 1, "category"=>"Hardware", "subcategory"=>"Personal Computer", "supplier" => "DELL", "particular"=>"Vostro 1320", "purchase_price"=>2300, "other_charges"=>500);
  5. $assetData[] = array("id" => 2, "category"=>"Hardware", "subcategory"=>"Personal Computer", "supplier" => "DELL", "particular"=>"Vostro 1420", "purchase_price"=>2500, "other_charges"=>0);
  6. $assetData[] = array("id" => 3, "category"=>"Hardware", "subcategory"=>"Laptop", "supplier" => "DELL", "particular"=>"Vostro 1520", "purchase_price"=>4500, "other_charges"=>100);
  7. $assetData[] = array("id" => 4, "category"=>"Hardware", "subcategory"=>"Laptop", "supplier" => "Acer", "particular"=>"Apire One", "purchase_price"=>2500, "other_charges"=>200);
  8. $assetData[] = array("id" => 5, "category"=>"Furniture", "subcategory"=>"Table", "supplier" => "CHEN", "particular"=>"Manager Table", "purchase_price"=>1000, "other_charges"=>0);
  9. $assetData[] = array("id" => 6, "category"=>"Furniture", "subcategory"=>"Table", "supplier" => "CHEN", "particular"=>"Staff Table", "purchase_price"=>500, "other_charges"=>0);
  10. $assetData[] = array("id" => 7, "category"=>"Furniture", "subcategory"=>"Chair", "supplier" => "CHEN", "particular"=>"Staff Chair", "purchase_price"=>500, "other_charges"=>0);
  11. $sorted = ArraySortUtil::multisort($assetData, array(
  12. array("field"=>"category"),
  13. array("field"=>"subcategory", "order" => true) // desc
  14. ));
  15. echo "<h1>multisort:result</h1>";
  16. echo "<pre>";
  17. print_r($sorted);
  18. echo "</pre>";
  19. $sorted = ArraySortUtil::uasort($assetData, array(
  20. array("field"=>"category"),
  21. array("field"=>"subcategory", "order" => true) // desc
  22. ));
  23. echo "<h1>uasort:result</h1>";
  24. echo "<pre>";
  25. print_r($sorted);
  26. echo "</pre>";

希望本文所述对大家的php程序设计有所帮助。