Php user-defined function for two-dimensional array sorting function, two-dimensional array sorting Function
This article describes how php user-defined functions can sort two-dimensional arrays. We will share this with you for your reference. The details are as follows:
/** Function: Two-dimensional array sorting function, supporting multi-key name sorting * return: sorted array * use: array_msort (array, key name to be sorted, sorting method); * example: array_msort ($ cflist, "chapter_orderid", "SORT_ASC"); * array_msort ($ arr, "name", "SORT_ASC", "type", "SORT_DESC", "size ", "SORT_ASC", "SORT_STRING"); */function array_msort ($ ArrayData, $ KeyName1, $ SortOrder1 = "SORT_ASC", $ SortType1 = "SORT_REGULAR") {if (! Is_array ($ ArrayData) {return $ ArrayData;} // obtain the number of parameters. $ ArgCount = func_num_args (); // sort and place it in the SortRule array for ($ I = 1; $ I <$ ArgCount; $ I ++) {$ Arg = func_get_arg ($ I); if (! Eregi ("SORT", $ Arg) {$ KeyNameList [] = $ Arg; $ SortRule [] = '$ '. $ Arg;} else {$ SortRule [] = $ Arg ;}// Get the values according to the keys and put them to array. foreach ($ ArrayData AS $ Key => $ Info) {foreach ($ KeyNameList AS $ KeyName) {$ KeyName} [$ Key] = $ Info [$ KeyName] ;}// Create the eval string and eval it. $ EvalString = 'array _ multisort ('. join (",", $ SortRule ). ', $ ArrayData);'; eval ($ EvalString); return $ ArrayData ;}