欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-25 15:01:58
本文实例讲述了php专用数组排序类arraysortutil用法。分享给大家供大家参考。具体如下: ** * arraysortutil is a array...

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

**
 * arraysortutil is a array sort utility, you can extends the sorting engine.
 *
 * @version 0.1
 * @package utility.sort
 */
class arraysortutil
{
 static function uasort($unsort, $fields)
 {
  if ( !is_array($unsort) || sizeof($unsort) <= 0 ) return $unsort;
  $sorted = uasortengine::uasort($unsort, $fields);
  return $sorted;
 }
 static function multisort($unsort, $fields)
 {
  if ( !is_array($unsort) || sizeof($unsort) <= 0 ) return $unsort;
  $sorted = multisortengine::multisort($unsort, $fields);
  return $sorted;
 }
}
class multisortengine
{
 static function multisort($unsort, $fields)
 {
  $sorted = $unsort;
  if (is_array($unsort))
  {
   $loadfields = array();
   foreach($fields as $sortfield)
   {
    $loadfields["field"][] = array(
        "name" => $sortfield["field"],
        "order" => $sortfield["order"],
        "nature" => $sortfield["nature"],
        "casesensitve" => $sortfield["casesensitve"]
    );
    $loadfields["data"][$field["field"]] = array();
   }
   // obtain a list of columns
   foreach ($sorted as $key => $row) {
    foreach($loadfields["field"] as $field) {
     $value = $row[$field["name"]];
     $loadfields["data"][$field["name"]][$key] = $value;
    }
   }
   $parameters = array();
   foreach($loadfields["field"] as $sortfield) {
    $array_data = $loadfields["data"][$sortfield["name"]];
    $casesensitve = ( $sortfield["casesensitve"] == null ) ? $sortfield["casesensitve"] : false;
    if (!$casesensitve) $array_data = array_map('strtolower', $array_data);
    $parameters[] = $array_data;
    if ( $sortfield["order"] != null ) $parameters[] = ( $sortfield["order"] ) ? sort_desc : sort_asc;
    if ( $sortfield["nature"] != null ) $parameters[] = ( $sortfield["nature"] ) ? sort_regular : sort_string;
   }
   $parameters[] = &$sorted;
   call_user_func_array("array_multisort", $parameters);
  }
  return $sorted;
 }
}
class uasortengine
{
 static private $casesensitve = false;
 static private $sortfields = array();
 static private $sortorder = true;
 static private $nature = false;
 static private function uasort_callback(&$a, &$b)
 {
  foreach(self::$sortfields as $sortfield)
  {
   $_field = $sortfield["field"];
   $_order = isset($sortfield["order"]) ? $sortfield["order"] : self::$sortorder;
   $_casesensitve = isset($sortfield["casesensitve"]) ? $sortfield["casesensitve"] : self::$casesensitve;
   $_nature = isset($sortfield["nature"]) ? $sortfield["nature"] : self::$nature;
   if ($_field != "")
   {
    $retval = 0;
    if ($_nature)
    {
     if ($_casesensitve)
     {
      $compare = strnatcmp($a[$_field], $b[$_field]);
     }
     else
     {
      $compare = strnatcasecmp($a[$_field], $b[$_field]);
     }
    }
    else
    {
     if ($_casesensitve)
     {
      $compare = strcmp($a[$_field], $b[$_field]);
     }
     else
     {
      $compare = strcasecmp($a[$_field], $b[$_field]);
     }
    }
    if ($compare !== 0 && !$_order) $compare = ($compare > 0) ? -1 : 1;
   }
   if ($compare !== 0) break;
  }
  return $compare;
 }
 static function uasort($unsort, $fields)
 {
  self::$sortfields = $fields;
  $sorted = $unsort;
  uasort($sorted, array('uasortengine', 'uasort_callback'));
  return $sorted;
 }
}

使用范例如下:

<?php
require_once('class.arraysortutil.php');
// declare test data
$assetdata[] = array("id" => 1, "category"=>"hardware", "subcategory"=>"personal computer", "supplier" => "dell", "particular"=>"vostro 1320", "purchase_price"=>2300, "other_charges"=>500);
$assetdata[] = array("id" => 2, "category"=>"hardware", "subcategory"=>"personal computer", "supplier" => "dell", "particular"=>"vostro 1420", "purchase_price"=>2500, "other_charges"=>0);
$assetdata[] = array("id" => 3, "category"=>"hardware", "subcategory"=>"laptop", "supplier" => "dell", "particular"=>"vostro 1520", "purchase_price"=>4500, "other_charges"=>100);
$assetdata[] = array("id" => 4, "category"=>"hardware", "subcategory"=>"laptop", "supplier" => "acer", "particular"=>"apire one", "purchase_price"=>2500, "other_charges"=>200);
$assetdata[] = array("id" => 5, "category"=>"furniture", "subcategory"=>"table", "supplier" => "chen", "particular"=>"manager table", "purchase_price"=>1000, "other_charges"=>0);
$assetdata[] = array("id" => 6, "category"=>"furniture", "subcategory"=>"table", "supplier" => "chen", "particular"=>"staff table", "purchase_price"=>500, "other_charges"=>0);
$assetdata[] = array("id" => 7, "category"=>"furniture", "subcategory"=>"chair", "supplier" => "chen", "particular"=>"staff chair", "purchase_price"=>500, "other_charges"=>0);
$sorted = arraysortutil::multisort($assetdata, array(
  array("field"=>"category"),
  array("field"=>"subcategory", "order" => true) // desc
 ));
echo "<h1>multisort:result</h1>";
echo "<pre>";
 print_r($sorted);
echo "</pre>";
$sorted = arraysortutil::uasort($assetdata, array(
  array("field"=>"category"),
  array("field"=>"subcategory", "order" => true) // desc
 ));
echo "<h1>uasort:result</h1>";
echo "<pre>";
 print_r($sorted);
echo "</pre>";

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