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.
CopyCode 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.Copy codeThe 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.Copy codeThe 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.