Use array_map and array_multisort to efficiently process multi-dimensional array sorting implementation code. The general method for sorting multi-dimensional arrays is
1. obtain the sorted data and put it into the array $ arrSort. The Middle-key index is the index of the array to be sorted to ensure uniqueness.
2. sort $ arrSort using sorting functions such as sort.
3. Traverse $ arrSort, obtain the data of the multi-dimensional array based on its index, and reconstruct the sorted multi-dimensional array.
Array ( [0] => Array ( [link] => test [name] => test.rpm [type] => file [size] => 988.9k [mtime] => 1185160178) .... )
I found a sorting function on the Internet a long time ago. it is not efficient, but practical.
_ Array_sort ($ arrFile, 1, 1); // Sort by name field _ array_sort ($ arrFile, 3, 1 ); // Sort by size field/* @ records the array to be sorted @ field the field to be sorted, note whether it is a number @ reverse positive or reverse */function _ array_sort ($ records, $ field, $ reverse, $ defaultSortField = 0) {$ uniqueSortId = 0; $ hash = array (); $ sortedRecords = array (); $ tempArr = array (); $ indexedArray = array (); $ recordArray = array (); foreach ($ records as $ record) {$ uniqueSortId ++; $ recordStr = Implode ("|", $ record ). "| ". $ uniqueSortId; $ recordArray [] = explode ("|", $ recordStr);} $ primarySortIndex = count ($ record); $ records = $ recordArray; foreach ($ records as $ record) {$ hash [$ record [$ primarySortIndex] = $ record [$ field];} uasort ($ hash, "strnatcasecmp "); if ($ reverse) $ hash = array_reverse ($ hash, true); $ valueCount = array_count_values ($ hash); foreach ($ hash as $ primaryKey => $ value ){ $ IndexedArray [] = $ primaryKey;} $ I = 0; foreach ($ hash as $ primaryKey => $ value) {$ I ++; if ($ valueCount [$ value]> 1) {foreach ($ records as $ record) {if ($ primaryKey = $ record [$ primarySortIndex]) {$ tempArr [$ record [$ defaultSortField]. "__". $ I] = $ record; break; }}$ index = array_search ($ primaryKey, $ indexedArray); if ($ I = count ($ records )) | ($ value! = $ Hash [$ indexedArray [$ index + 1]) {uksort ($ tempArr, "strnatcasecmp"); if ($ reverse) $ tempArr = array_reverse ($ tempArr ); foreach ($ tempArr as $ newRecs) {$ sortedRecords [] = $ newRecs;} $ tempArr = array () ;}} else {foreach ($ records as $ record) {if ($ primaryKey = $ record [$ primarySortIndex]) {$ sortedRecords [] = $ record; break ;}}} return $ sortedRecords ;}II sort by array_map and array_mutisort
Array_mutisort can also be sorted twice or three times based on multiple values, which is incomparable to the previous function.
Use array_map to obtain the array to be sorted by $ arrField = array_map (create_function ('$ n', 'return $ n ["size"];'), $ arrFile ); // Sort by array_mutisort $ array_multisort ($ arrField, SORT_DESC, $ arrFile );
III final test
Test with an array of 188 pieces of data, sort 50 times to calculate the average value.
Method 1
0.04269016 name
0.04267142 size
Method 2
0.001249 name
0.00083924 size
The result is self-evident.
For more articles about efficient processing of multi-dimensional array sorting by php array_map array_multisort, refer to PHP Chinese network!