时间:2021-07-01 10:21:17 帮助过:18人阅读
代码如下:
Array
(
[0] => Array
(
[link] => test
[name] => test.rpm
[type] => file
[size] => 988.9k
[mtime] => 1185160178)
....
)
代码如下:
_array_sort($arrFile, 1, 1);//根据name字段排序
_array_sort($arrFile, 3, 1);//根据size字段排序
/*
@records 要排序的数组
@field要排序的字段,注意是数字
@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;
}
代码如下:
利用array_map获取要依据排序的数组
$arrField = array_map(create_function('$n', 'return $n["size"];'), $arrFile);
//利用array_mutisort来进行排序
$array_multisort($arrField, SORT_DESC, $arrFile);
以上就介绍了muv-luv(alternative) php array_map array_multisort 高效处理多维数组排序,包括了muv-luv(alternative)方面的内容,希望对PHP教程有兴趣的朋友有所帮助。