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.
The code is as follows:
Array
(
[0] => Array
(
[Link] => test
[Name] => test. rpm
[Type] => file
[Size] = & gt; 988.9 k
[Mtime] = & gt; 1185160178)
....
)
I found a sorting function on the Internet a long time ago. it is not efficient, but practical.
The code is as follows:
_ Array_sort ($ arrFile, 1, 1); // Sort by name field
_ Array_sort ($ arrFile, 3, 1); // Sort by size field
/*
@ Records: array to be sorted
@ Field the field to be sorted. Note that 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.
The code is as follows:
Use array_map to obtain the array to be sorted
$ 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.