php 专用数组排序类用法示例

内容摘要
这篇文章主要为大家详细介绍了php 专用数组排序类用法示例,具有一定的参考价值,可以用来参考一下。

对php专用数组排序的类 ArraySortUtil对此感兴趣的朋友,看看idc笔记做的技
文章正文

这篇文章主要为大家详细介绍了php 专用数组排序类用法示例,具有一定的参考价值,可以用来参考一下。

对php专用数组排序的类 ArraySortUtil对此感兴趣的朋友,看看idc笔记做的技术笔记!

/**
 * php专用数组排序的类 ArraySortUtil
 * @param 
 * @author php教程 www.idcnote.com
 * 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教程(www.idcnote.com)   ***/

注:关于php 专用数组排序类用法示例的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!