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 相关文章推荐
社区(php&amp;&amp;mysql)二
Oct 09 PHP
模仿OSO的论坛(五)
Oct 09 PHP
据说是雅虎的一份PHP面试题附答案
Jan 07 PHP
phpmyadmin里面导入sql语句格式的大量数据的方法
Jun 05 PHP
PHP 杂谈《重构-改善既有代码的设计》之四 简化条件表达式
Apr 09 PHP
基于php 随机数的深入理解
Jun 05 PHP
50个PHP程序性能优化的方法
Jun 02 PHP
destoon调用自定义模板及样式的公告栏
Jun 21 PHP
php通过baihui网API实现读取word文档并展示
Jun 22 PHP
PHP Callable强制指定回调类型的方法
Aug 30 PHP
PHP入门教程之图像处理技巧分析
Sep 11 PHP
PHP getName()函数讲解
Feb 03 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显示MySQL数据的三种方法
2008/06/05 PHP
php读取msn上的用户信息类
2008/12/05 PHP
《PHP编程最快明白》第七讲:php图片验证码与缩略图
2010/11/01 PHP
PHP获取数组中重复最多的元素的实现方法
2014/11/11 PHP
Yii中Model(模型)的创建及使用方法
2015/12/28 PHP
PHP操作FTP类 (上传、下载、移动、创建等)
2016/03/31 PHP
php+MySQL实现登录时验证登录名和密码是否正确
2016/05/10 PHP
PHP strip_tags保留多个HTML标签的方法
2016/05/22 PHP
thinkPHP3.2.2框架行为扩展及demo示例
2018/06/19 PHP
js滚动条回到顶部的代码
2011/12/06 Javascript
基于jquery的文本框与autocomplete结合使用(asp.net+json)
2012/05/30 Javascript
javascript标签在页面中的位置探讨
2013/04/11 Javascript
js播放wav文件(源码)
2013/04/22 Javascript
简介JavaScript中的sub()方法的使用
2015/06/08 Javascript
js将json格式的对象拼接成复杂的url参数方法
2016/05/25 Javascript
详解Vue2 无限级分类(添加,删除,修改)
2017/03/07 Javascript
jQuery动态添加.active 实现导航效果代码思路详解
2017/08/29 jQuery
JS中的回调函数实例浅析
2018/03/21 Javascript
使用elementUI实现将图片上传到本地的示例
2018/09/04 Javascript
Angularjs之ngModel中的值验证绑定方法
2018/09/13 Javascript
js实现拾色器插件(ColorPicker)
2020/05/21 Javascript
python实现每次处理一个字符的三种方法
2014/10/09 Python
在Python中处理XML的教程
2015/04/29 Python
Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法
2015/05/16 Python
Python使用Redis实现作业调度系统(超简单)
2016/03/22 Python
基于使用paramiko执行远程linux主机命令(详解)
2017/10/16 Python
Python爬虫之urllib基础用法教程
2019/10/12 Python
使用Django和Postgres进行全文搜索的实例代码
2020/02/13 Python
python 工具 字符串转numpy浮点数组的实现
2020/03/14 Python
Python基于当前时间批量创建文件
2020/05/07 Python
Python操作dict时避免出现KeyError的几种解决方法
2020/09/20 Python
工作中个人的自我评价
2013/12/31 职场文书
摄影专业毕业生求职信
2014/08/05 职场文书
教你修复 Win11应用商店加载空白问题
2021/12/06 数码科技
python flappy bird小游戏分步实现流程
2022/02/15 Python
使用Ajax实现无刷新上传文件
2022/04/12 Javascript