To sort multidimensional arrays, the common practice is to
1 Gets the sorted data and puts it into the array $arrsort. Where the key index is the index to sort the array, ensuring uniqueness
2 sort the $arrsort using sort function sort.
3 iterates through the $arrsort, gets the data of the multidimensional array according to its index, and reconstructs the sorted multidimensional array.
Copy the 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 a sort of function, not efficient, but very practical
Copy the 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 to sort, note that the numbers
@reverse Positive or reverse order
*/
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 two or three times based on multiple values, which cannot be compared to the previous function.
Copy the Code code as follows:
Use Array_map to get an array to sort on
$arrField = Array_map (create_function (' $n ', ' return $n ["size"]; '), $arrFile);
Use Array_mutisort to sort
$array _multisort ($arrField, Sort_desc, $arrFile);
III final Test
The array of 188 data is tested and sorted 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
The above describes the Muv-luv (alternative) PHP array_map array_multisort Efficient processing of multidimensional array ordering, including muv-luv (alternative) aspects of the content, You want to be helpful to a friend who is interested in PHP tutorials.