本文实例讲述了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 ) "caseSensitve"] : false; if (!$caseSensitve) $array_data = array_map('strtolower', $array_data); $parameters[] = $array_data; if ( $sortfield["order"] != null ) $parameters[] = ( $sortfield["order"] ) "nature"] != null ) $parameters[] = ( $sortfield["nature"] ) "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"]) "order"] : self::$sortorder; $_caseSensitve = isset($sortfield["caseSensitve"]) "caseSensitve"] : self::$caseSensitve; $_nature = isset($sortfield["nature"]) "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) "htmlcode"><"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程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 群星《天域原音·聆听雪域藏歌STS+SRS》[WAV+CUE]
- 尤长靖.2020-AZORAland·我是尤长靖【香蕉娱乐】【FLAC分轨】
- 群星.1995-台北爱情故事【飞碟】【WAV+CUE】
- 群星.2024-锦绣安宁电视剧原声带【D-Jin.Music】【FLAC分轨】
- 群星《闪光的夏天 第4期》[320K/MP3][97.82MB]
- 群星《闪光的夏天 第4期》[FLAC/分轨][553.31MB]
- 群星《奔赴!万人现场 第4期》[320K/MP3][80.75MB]
- 林琳《独角戏HQ》WAV
- FIM-《Super-Sound-3》声霸3[WAV+CUE]
- 喇叭花-绝版天碟《我的碟“MyDisc”》[正版原抓WAV+CUE]
- 陈慧琳.1999-真感觉【正东】【WAV+CUE】
- 徐玮.1986-走自己的路(喜玛拉雅复刻版)【同心圆】【WAV+CUE】
- 林海峰.2003-我撑你【EMI百代】【WAV+CUE】
- 群星《奔赴!万人现场 第4期》[FLAC/分轨][454.89MB]
- 腾讯音乐人《未来立体声·Stereo Future VOL.12》[320K/MP3][62.37MB]