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


Posted in PHP onApril 03, 2015

本文实例讲述了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 相关文章推荐
用header 发送cookie的php代码
Mar 16 PHP
php magic_quotes_gpc的一点认识与分析
Aug 18 PHP
PHP Pear 安装及使用
Mar 19 PHP
PHP文件生成的图片无法使用CDN缓存的解决方法
Jun 20 PHP
php实现zip文件解压操作
Nov 03 PHP
Yii的Srbac插件用法详解
Jul 14 PHP
PHP对象实例化单例方法
Jan 19 PHP
一个实用的php验证码类
Jul 06 PHP
PHP实现mysqli批量执行多条语句的方法示例
Jul 22 PHP
OAuth认证协议中的HMACSHA1加密算法(实例)
Oct 25 PHP
php回调函数处理数组操作示例
Apr 13 PHP
PHP设计模式(六)桥连模式Bridge实例详解【结构型】
May 02 PHP
Win7下手动安装apache2.2、php5.4笔记
Apr 03 #PHP
PHP中返回引用类型的方法
Apr 03 #PHP
PHP中应该避免使用同名变量(拆分临时变量)
Apr 03 #PHP
PHP5多态性与动态绑定介绍
Apr 03 #PHP
php5.4以上版本GBK编码下htmlspecialchars输出为空问题解决方法汇总
Apr 03 #PHP
Windows下编译PHP5.4和xdebug全记录
Apr 03 #PHP
Centos下升级php5.2到php5.4全记录(编译安装)
Apr 03 #PHP
You might like
PHP 作用域解析运算符(::)
2010/07/27 PHP
Yii2框架RESTful API 格式化响应,授权认证和速率限制三部分详解
2016/11/10 PHP
PHP封装cURL工具类与应用示例
2019/07/01 PHP
jQuery EasyUI API 中文文档 - TreeGrid 树表格使用介绍
2011/11/21 Javascript
javascript分页代码(当前页码居中)
2012/09/20 Javascript
整理的比较全的event对像在ie与firefox浏览器中的区别
2013/11/25 Javascript
简单的邮箱登陆的提示效果类似于yahoo邮箱
2014/02/26 Javascript
localResizeIMG先压缩后使用ajax无刷新上传(移动端)
2015/08/11 Javascript
javascript每日必学之条件分支
2016/02/17 Javascript
JS中的进制转换以及作用
2016/06/26 Javascript
必备的JS调试技巧汇总
2016/07/20 Javascript
理解Angular的providers给Http添加默认headers
2017/07/04 Javascript
react系列从零开始_简单谈谈react
2017/07/06 Javascript
详解jQuery同步Ajax带来的UI线程阻塞问题及解决办法
2017/08/09 jQuery
Angularjs中数据绑定的实例详解
2017/08/25 Javascript
Vue核心概念Getter的使用方法
2019/01/18 Javascript
JavaScript实现单图片上传并预览功能
2019/09/30 Javascript
JavaScript 链表定义与使用方法示例
2020/04/28 Javascript
Python实现的爬虫刷回复功能示例
2018/06/07 Python
python实现旋转和水平翻转的方法
2018/10/25 Python
解决python中 f.write写入中文出错的问题
2018/10/31 Python
python3 实现对图片进行局部切割的方法
2018/12/05 Python
Python第三方库face_recognition在windows上的安装过程
2019/05/03 Python
Django框架中间件(Middleware)用法实例分析
2019/05/24 Python
Python使用pyyaml模块处理yaml数据
2020/04/14 Python
Python 3.9的到来到底是意味着什么
2020/10/14 Python
Html5适配iphoneX刘海屏的简单实现
2019/04/09 HTML / CSS
Farah官方网站:男士服装及配件
2019/11/01 全球购物
货车司机岗位职责
2014/03/18 职场文书
巴西世界杯32强口号
2014/06/05 职场文书
校园文化标语
2014/06/18 职场文书
大学英语专业求职信
2014/06/21 职场文书
科级干部群众路线教育实践活动个人对照检查材料
2014/09/19 职场文书
离婚协议书范本(通用篇)
2014/11/30 职场文书
辞职信模板(中英文版)
2015/02/27 职场文书
Nginx+Tomcat实现负载均衡、动静分离的原理解析
2021/03/31 Servers