PHP array_map array_multisort Efficient handling of multidimensional array sorting _php techniques

Source: Internet
Author: User
To sort multidimensional arrays, the common practice is to
1 Gets the data that is sorted and puts it into an array $arrsort. Where the key index is the index of the array to be sorted, guaranteed uniqueness
2 sort the $arrsort by using sort functions.
3 traversal $arrsort, according to its index, obtains the multidimensional array the data, reconstructs the sorted multidimensional array.
Copy Code code as follows:

Array
(
[0] => Array
(
[link] => test
[Name] => test.rpm
[Type] => file
[Size] => 988.9k
[Mtime] => 1185160178)
....
)

I found a long time ago on the internet to find a sort of function, not efficient, but very practical
Copy Code code as follows:

_array_sort ($arrFile, 1, 1);//Sort by Name field
_array_sort ($arrFile, 3, 1);//Sort by Size field
/*
@records the array to sort
@field The fields you want to sort, note that the numbers
@reverse positive sequence or reverse sequence
*/
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 use Array_map and array_mutisort to sort
Array_mutisort can also be sorted two or three times based on multiple values, which cannot be compared to the previous function.
Copy Code code as follows:

Use Array_map to get an array to sort by
$arrField = Array_map (create_function (' $n ', ' return $n [' size ']; '), $arrFile);
Using Array_mutisort to sort
$array _multisort ($arrField, Sort_desc, $arrFile);

III final Test
Test with an array of 188 data, sorting 50 times to average.
The first way
0.04269016 Name
0.04267142 size
The second way
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.