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

本文实例讲述了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程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php
, 数组排序类
ArraySortUtil
java.util.array.sort、js array sort排序、array sort 排序、listsortutil、array multisort,以便于您获取更多的相关知识。

时间: 2024-10-29 05:59:37

php专用数组排序类ArraySortUtil用法实例_php技巧的相关文章

PHP多线程类及用法实例_php技巧

本文实例讲述了PHP多线程类及用法.分享给大家供大家参考.具体分析如下: 一般来说可通过WEB服务器来实现PHP多线程功能,当然,对多线程有深入理解的人都知道通过WEB服务器实现的多线程只能模仿多线程的一些效果,并不是真正意义上的多线程. 但不管怎么样,它还是能满足我们的一些需要的,在需要类似多线程的功能方面还是可以采用这个类,代码如下: 复制代码 代码如下: /** * @title: PHP多线程类(Thread) * @version: 1.0 * * PHP多线程应用示例: * requ

PHP中Memcache操作类及用法实例_php技巧

本文实例讲述了PHP中Memcache操作类及用法.分享给大家供大家参考.具体分析如下: 复制代码 代码如下: <?php      /*  内存缓存管理      */ class Yc_Memcache{   private $memcache=null;       public function __construct(){   }   /**      * 连接数据库      *      * @param mixed $host      * @param mixed $port 

php类的定义与继承用法实例_php技巧

本文实例讲述了php类的定义与继承用法.分享给大家供大家参考.具体如下: <?php /* * class */ class people { public $name; public $age; function __construct($namec,$agec) { $this->name = $namec; $this->age = $agec; } protected function getmessage() { return "姓名:".$this->

php获得网站访问统计信息类Compete API用法实例_php技巧

本文实例讲述了php获得网站访问统计信息类Compete API用法.分享给大家供大家参考.具体如下: 这里使用php获得网站访问统计信息类Compete API,Compete是一个专门用来统计网站信息的网站 <?php // Check for dependencies if (!function_exists('curl_init')) throw new Exception('Compete needs the CURL PHP extension.'); if (!function_e

php过滤html标记属性类用法实例_php技巧

本文实例讲述了php 过滤html标记属性类及其用法.是PHP项目开发中比较常见的实用技巧.分享给大家供大家参考.具体方法如下: HtmlAttributeFilter.class.php类文件如下: <?php /** HTML Attribute Filter * Date: 2013-09-22 * Author: fdipzone * ver: 1.0 * * Func: * public strip 过滤属性 * public setAllow 设置允许的属性 * public set

php文件压缩之PHPZip类用法实例_php技巧

本文实例讲述了php文件压缩之PHPZip类用法.分享给大家供大家参考.具体如下: <?php // // PHPZip v1.2 by Sext (sext@neud.net) // // Makes zip archive // // Based on "Zip file creation class", uses zLib // // class PHPZip { function Zip($dir, $zipfilename) { if (@function_exists

php广告加载类用法实例_php技巧

本文实例讲述了php广告加载类的用法,非常实用.分享给大家供大家参考.具体方法如下: 该php广告加载类,支持异步与同步加载.需要使用Jquery实现. ADLoader.class.php类文件如下: <?php /** 广告加载管理类 * Date: 2013-08-04 * Author: fdipzone * Ver: 1.0 * * Func: * public load 加载广告集合 * public setConfig 广告配置 * private getAds 根据channel

PHP获取文件夹大小函数用法实例_php技巧

本文实例讲述了PHP获取文件夹大小函数用法.分享给大家供大家参考.具体如下: <?php // 获取文件夹大小 function getDirSize($dir) { $handle = opendir($dir); while (false!==($FolderOrFile = readdir($handle))) { if($FolderOrFile != "." && $FolderOrFile != "..") { if(is_dir(

php中动态变量用法实例_php技巧

本文实例讲述了php中动态变量用法.分享给大家供大家参考.具体分析如下: 定义的固定变量: $my_pic_1=$row["pic_1"]; $my_pic_2=$row["pic_2"]; $my_pic_3=$row["pic_3"]; $my_pic_4=$row["pic_4"]; $my_pic_5=$row["pic_5"]; $my_pic_6=$row["pic_6"];