This article mainly introduces the usage of php-specific array sorting class ArraySortUtil. The example analyzes the methods and corresponding skills of ArraySortUtil for Array sorting. For more information, see
This article mainly introduces the usage of php-specific array sorting class ArraySortUtil. The example analyzes the methods and corresponding skills of ArraySortUtil for Array sorting. For more information, see
This article describes the usage of php-specific array sorting class ArraySortUtil. Share it with you for your reference. The details are as follows:
* ** 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 columnsforeach ($ 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 ["dat A "] [$ 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 = $ sortfi Eld ["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 ;}}
An example is as follows:
<? Phprequire_once ('class. arraySortUtil. php '); // declare test data $ assetData [] = array ("id" => 1, "category" => "Hardware ", "subcategory" => "Personal Computer", "supplier" => "DELL", "participant" => "Vostro 1320", "purchase_price" => 2300, "other_charges" => 500); $ assetData [] = array ("id" => 2, "category" => "Hardware ", "subcategory" => "Personal Computer", "supplier" => "DELL", "participant" => "Vostro 1420", "purchase_price" => 2500, "other_charges" => 0); $ assetData [] = array ("id" => 3, "category" => "Hardware", "subcategory" => "Laptop ", "supplier" => "DELL", "participant" => "Vostro 1520", "purchase_price" => 4500, "other_charges" => 100 ); $ assetData [] = array ("id" => 4, "category" => "Hardware", "subcategory" => "Laptop ", "supplier" => "Acer", "participant" => "Apire One", "purchase_price" => 2500, "other_charges" => 200 ); $ assetData [] = array ("id" => 5, "category" => "Furniture", "subcategory" => "Table ", "supplier" => "CHEN", "participant" => "Manager Table", "purchase_price" => 1000, "other_charges" => 0 ); $ assetData [] = array ("id" => 6, "category" => "Furniture", "subcategory" => "Table ", "supplier" => "CHEN", "participant" => "Staff Table", "purchase_price" => 500, "other_charges" => 0 ); $ assetData [] = array ("id" => 7, "category" => "Furniture", "subcategory" => "Chair ", "supplier" => "CHEN", "participant" => "Staff Chair", "purchase_price" => 500, "other_charges" => 0); $ sorted = ArraySortUtil :: multisort ($ assetData, array ("field" => "category"), array ("field" => "subcategory", "order" => true) // desc); echo "multisort: result"; echo"
"; print_r($sorted);echo "
"; $ Sorted = ArraySortUtil: uasort ($ assetData, array (" field "=>" category "), array (" field "=>" subcategory ", "order" => true) // desc); echo "uasort: result"; echo"
"; print_r($sorted);echo "
";
I hope this article will help you with php programming.
,