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下用rmdir实现删除目录的三种方法小结
Apr 20 PHP
PHP MVC模式在网站架构中的实现分析
Mar 04 PHP
使用php shell命令合并图片的代码
Jun 23 PHP
完善CodeIgniter在IDE中代码提示功能的方法
Jul 19 PHP
ThinkPHP调用common/common.php函数提示错误function undefined的解决方法
Aug 25 PHP
初识PHP
Sep 28 PHP
详解PHP中的Traits
Jul 29 PHP
浅谈php中urlencode与rawurlencode的区别
Sep 05 PHP
php 三大特点:封装,继承,多态
Feb 19 PHP
PHP实现的mongoDB数据库操作类完整实例
Apr 10 PHP
使用Zookeeper分布式部署PHP应用程序
Mar 15 PHP
PHP设计模式(七)组合模式Composite实例详解【结构型】
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中的string类型使用说明
2010/07/27 PHP
PHP的password_hash()使用实例
2014/03/17 PHP
PHP sleep()函数, usleep()函数
2016/08/25 PHP
php版微信公众号自定义分享内容实现方法
2016/09/22 PHP
jquery 页面全选框实践代码
2010/04/02 Javascript
js实现网页倒计时、网站已运行时间功能的代码3例
2014/04/14 Javascript
面向切面编程(AOP)的理解
2015/05/01 Javascript
js实现每日自动换一张图片的方法
2015/05/04 Javascript
JS实现跟随鼠标立体翻转图片的方法
2015/05/04 Javascript
关于JavaScript限制字数的输入框的那些事
2016/08/14 Javascript
IE8利用自带的setCapture和releaseCapture解决iframe的拖拽事件方法
2016/10/25 Javascript
nodejs入门教程二:创建一个简单应用示例
2017/04/24 NodeJs
原生JS控制多个滚动条同步跟随滚动效果
2017/12/22 Javascript
nodejs读取本地中文json文件出现乱码解决方法
2018/10/10 NodeJs
微信小程序实现发送验证码按钮效果
2018/12/20 Javascript
使用JS判断页面是首次被加载还是刷新
2019/05/26 Javascript
基于JS实现简单滑块拼图游戏
2019/10/12 Javascript
Django中更新多个对象数据与删除对象的方法
2015/07/17 Python
python 默认参数问题的陷阱
2016/02/29 Python
Python备份目录及目录下的全部内容的实现方法
2016/06/12 Python
Python的Tornado框架实现图片上传及图片大小修改功能
2016/06/30 Python
在 Python 应用中使用 MongoDB的方法
2017/01/05 Python
Python实现螺旋矩阵的填充算法示例
2017/12/28 Python
python根据list重命名文件夹里的所有文件实例
2018/10/25 Python
利用Python将数值型特征进行离散化操作的方法
2018/11/06 Python
python+openCV利用摄像头实现人员活动检测
2019/06/22 Python
python pytest进阶之conftest.py详解
2019/06/27 Python
Django实现文件上传和下载功能
2019/10/06 Python
Python爬虫使用浏览器cookies:browsercookie过程解析
2019/10/22 Python
基于Python和PyYAML读取yaml配置文件数据
2020/01/13 Python
德国宠物用品、宠物食品及水族馆网上商店:ZooRoyal
2017/07/09 全球购物
美国知名珠宝首饰品牌:Gemvara
2017/10/06 全球购物
广州盈通面试题
2015/12/05 面试题
财务人员个人求职信范文
2013/12/04 职场文书
《中华少年》教学反思
2014/02/15 职场文书
python3读取文件指定行的三种方法
2021/05/24 Python