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代码中使用换行及(\n或\r\n和br)的应用
Feb 02 PHP
深入mysql_fetch_row()与mysql_fetch_array()的区别详解
Jun 05 PHP
php页面防重复提交方法总结
Nov 25 PHP
PHP中的日期加减方法示例
Aug 21 PHP
PHP上传文件时自动分配路径的方法
Jan 09 PHP
试用php中oci8扩展
Jun 18 PHP
PHP测试成功的邮件发送案例
Oct 26 PHP
PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码
Mar 25 PHP
PHP实现递归目录的5种方法
Oct 27 PHP
Yii2框架redis基本应用示例
Jul 13 PHP
YII2框架中actions的作用与使用方法示例
Mar 13 PHP
php实现将数组或对象写入到文件的方法小结【三种方法】
Apr 22 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
Laravel框架模板继承操作示例
2018/06/11 PHP
基于jquery的图片的切换(以数字的形式)
2011/02/14 Javascript
输入框的字数时时统计—关于 onpropertychange 和 oninput 使用
2011/10/21 Javascript
js不完美解决click和dblclick事件冲突问题
2012/07/16 Javascript
jQuery操作input值的各种方法总结
2013/11/21 Javascript
使用C++为node.js写扩展模块
2015/04/22 Javascript
apply和call方法定义及apply和call方法的区别
2015/11/15 Javascript
跟我学习javascript的prototype使用注意事项
2015/11/17 Javascript
详解javascript实现自定义事件
2016/01/19 Javascript
JS获取当前脚本文件的绝对路径
2016/03/02 Javascript
动态生成的DOM不会触发onclick事件的原因及解决方法
2016/08/06 Javascript
ReactNative列表ListView的用法
2017/08/02 Javascript
详解webpack2+node+react+babel实现热加载(hmr)
2017/08/24 Javascript
nodejs基于mssql模块连接sqlserver数据库的简单封装操作示例
2018/01/05 NodeJs
小程序开发基础之view视图容器
2018/08/21 Javascript
微信小程序购物车、父子组件传值及calc的注意事项总结
2018/11/14 Javascript
微信JS-SDK updateAppMessageShareData安卓不能自定义分享详解
2019/03/29 Javascript
JavaScript变量作用域及内存问题实例分析
2019/06/10 Javascript
详解Vue Cli浏览器兼容性实践
2020/06/08 Javascript
[04:46]2018年度玩家喜爱的电竞媒体-完美盛典
2018/12/16 DOTA
[42:32]完美世界DOTA2联赛循环赛 Magma vs PXG BO2第二场 10.28
2020/10/28 DOTA
Python生成pdf文件的方法
2014/08/04 Python
python处理csv数据的方法
2015/03/11 Python
python实现简单的socket server实例
2015/04/29 Python
python实现比较两段文本不同之处的方法
2015/05/30 Python
Python的matplotlib绘图如何修改背景颜色的实现
2019/07/16 Python
python自动保存百度盘资源到百度盘中的实例代码
2019/08/26 Python
图解python全局变量与局部变量相关知识
2019/11/02 Python
Python爬虫实现HTTP网络请求多种实现方式
2020/06/19 Python
采用专利算法搜索最廉价的机票:CheapAir
2016/09/10 全球购物
维多利亚的秘密阿联酋官网:Victoria’s Secret阿联酋
2019/12/07 全球购物
军训心得体会
2013/12/31 职场文书
建筑设计学生的自我评价
2014/01/16 职场文书
家长会学生演讲稿
2014/04/26 职场文书
Python源码解析之List
2021/05/21 Python
redis中lua脚本使用教程
2021/11/01 Redis