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项目打包方法
Feb 18 PHP
PHP 长文章分页函数 带使用方法,不会分割段落,翻页在底部
Oct 22 PHP
php inc文件使用的风险和注意事项
Nov 12 PHP
11个PHPer必须要了解的编程规范
Sep 22 PHP
PHP使用in_array函数检查数组中是否存在某个值
Mar 25 PHP
PHP中使用hidef扩展代替define提高性能
Apr 09 PHP
简单解决新浪SAE无法上传文件的问题
May 13 PHP
整理php防注入和XSS攻击通用过滤
Sep 13 PHP
Yii2 rbac权限控制之rule教程详解
Jun 23 PHP
PHP中字符与字节的区别及字符串与字节转换示例
Oct 15 PHP
php发送http请求的常用方法分析
Nov 08 PHP
探究Laravel使用env函数读取环境变量为null的问题
Dec 06 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
ThinkPHP 404页面的设置方法
2015/01/14 PHP
PHP自定义多进制的方法
2016/11/03 PHP
TP5框架安全机制实例分析
2020/04/05 PHP
JavaScript去掉数组中的重复元素
2011/01/13 Javascript
限制textbox或textarea输入字符长度的JS代码
2013/10/16 Javascript
关于Javascript 对象(object)的prototype
2014/05/09 Javascript
Jquery给基本控件的取值、赋值示例
2014/05/23 Javascript
js实现带按钮的上下滚动效果
2015/05/12 Javascript
js+html5实现canvas绘制简单矩形的方法
2015/06/05 Javascript
javascript中substring()、substr()、slice()的区别
2015/08/30 Javascript
原生JS实现导航下拉菜单效果
2020/11/25 Javascript
nodejs个人博客开发第七步 后台登陆
2017/04/12 NodeJs
js canvas实现适用于移动端的百分比仪表盘dashboard
2017/07/18 Javascript
JS设计模式之惰性模式(二)
2017/09/29 Javascript
nodeJS微信分享
2017/12/20 NodeJs
详解vue-cli 构建项目 vue-cli请求后台接口 vue-cli使用axios、sass、swiper
2018/05/28 Javascript
JavaScript引用类型RegExp基本用法详解
2018/08/09 Javascript
通过实践编写优雅的JavaScript代码
2019/05/30 Javascript
layui问题之模拟table表格中的选中按钮选中事件的方法
2019/09/20 Javascript
vue实现将数据存入vuex中以及从vuex中取出数据
2019/11/08 Javascript
javascript实现弹幕墙效果
2019/11/28 Javascript
[51:14]LGD vs VP 2018国际邀请赛淘汰赛BO3 第一场 8.21
2018/08/22 DOTA
使用pyecharts无法import Bar的解决方案
2020/04/23 Python
使用Python的OpenCV模块识别滑动验证码的缺口(推荐)
2019/05/10 Python
在django-xadmin中APScheduler的启动初始化实例
2019/11/15 Python
Python中logger日志模块详解
2020/08/04 Python
产品销售员岗位职责
2013/12/18 职场文书
学校通报表扬范文
2015/05/04 职场文书
违规违纪检讨书范文
2015/05/06 职场文书
党员转正申请报告
2015/05/15 职场文书
2016年“我们的节日·端午节”活动总结
2016/04/01 职场文书
2016年社区中秋节活动总结
2016/04/05 职场文书
python生成随机数、随机字符、随机字符串
2021/04/06 Python
python实现层次聚类的方法
2021/11/01 Python
SpringDataJPA在Entity中常用的注解介绍
2021/12/06 Java/Android
vue实现拖拽交换位置
2022/04/07 Vue.js