php专用数组排序类ArraySortUtil用法实例
本文实例讲述了php专用数组排序类ArraySortUtil用法。分享给大家供大家参考。具体如下:
** *ArraySortUtilisaarraysortutility,youcanextendsthesortingengine. * *@version0.1 *@packageutility.sort */ classArraySortUtil { staticfunctionuasort($unsort,$fields) { if(!is_array($unsort)||sizeof($unsort)<=0)return$unsort; $sorted=uasortEngine::uasort($unsort,$fields); return$sorted; } staticfunctionmultisort($unsort,$fields) { if(!is_array($unsort)||sizeof($unsort)<=0)return$unsort; $sorted=multisortEngine::multisort($unsort,$fields); return$sorted; } } classmultisortEngine { staticfunctionmultisort($unsort,$fields) { $sorted=$unsort; if(is_array($unsort)) { $loadFields=array(); foreach($fieldsas$sortfield) { $loadFields["field"][]=array( "name"=>$sortfield["field"], "order"=>$sortfield["order"], "nature"=>$sortfield["nature"], "caseSensitve"=>$sortfield["caseSensitve"] ); $loadFields["data"][$field["field"]]=array(); } //Obtainalistofcolumns foreach($sortedas$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; } } classuasortEngine { staticprivate$caseSensitve=false; staticprivate$sortfields=array(); staticprivate$sortorder=true; staticprivate$nature=false; staticprivatefunctionuasort_callback(&$a,&$b) { foreach(self::$sortfieldsas$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; } staticfunctionuasort($unsort,$fields) { self::$sortfields=$fields; $sorted=$unsort; uasort($sorted,array('uasortEngine','uasort_callback')); return$sorted; } }
使用范例如下:
<?php require_once('class.ArraySortUtil.php'); //declaretestdata $assetData[]=array("id"=>1,"category"=>"Hardware","subcategory"=>"PersonalComputer","supplier"=>"DELL","particular"=>"Vostro1320","purchase_price"=>2300,"other_charges"=>500); $assetData[]=array("id"=>2,"category"=>"Hardware","subcategory"=>"PersonalComputer","supplier"=>"DELL","particular"=>"Vostro1420","purchase_price"=>2500,"other_charges"=>0); $assetData[]=array("id"=>3,"category"=>"Hardware","subcategory"=>"Laptop","supplier"=>"DELL","particular"=>"Vostro1520","purchase_price"=>4500,"other_charges"=>100); $assetData[]=array("id"=>4,"category"=>"Hardware","subcategory"=>"Laptop","supplier"=>"Acer","particular"=>"ApireOne","purchase_price"=>2500,"other_charges"=>200); $assetData[]=array("id"=>5,"category"=>"Furniture","subcategory"=>"Table","supplier"=>"CHEN","particular"=>"ManagerTable","purchase_price"=>1000,"other_charges"=>0); $assetData[]=array("id"=>6,"category"=>"Furniture","subcategory"=>"Table","supplier"=>"CHEN","particular"=>"StaffTable","purchase_price"=>500,"other_charges"=>0); $assetData[]=array("id"=>7,"category"=>"Furniture","subcategory"=>"Chair","supplier"=>"CHEN","particular"=>"StaffChair","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程序设计有所帮助。