-
php专用数组排序类ArraySortUtil用法实例
这篇文章主要介绍了php专用数组排序类ArraySortUtil用法,实例分析了ArraySortUtil实现数组排序的方法与对应使用技巧,需要的朋友可以参考下。
本文实例讲述了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程序设计有所帮助。
出处:http://www.phpfensi.com/php/20210521/15658.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式