PHP array_map array_multisort efficient processing of multi-dimensional array sorting

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.