Created
September 7, 2017 01:58
-
-
Save lepig/3ec3e7031039825bed90f9901a956d67 to your computer and use it in GitHub Desktop.
PHP多维数组按不同字段进行排序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$array=[ | |
['name'=>'张三','age'=>'23'], | |
['name'=>'李四','age'=>'64'], | |
['name'=>'王五','age'=>'55'], | |
['name'=>'赵六','age'=>'66'], | |
['name'=>'孙七','age'=>'17'], | |
]; | |
$sort = array( | |
'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序 | |
'field' => 'age', //排序字段 | |
); | |
$arrSort = array(); | |
foreach($array as $uniqid => $row){ | |
foreach($row AS $key=>$value){ | |
$arrSort[$key][$uniqid] = $value; | |
} | |
} | |
array_multisort($arrSort[$sort['field']], constant($sort['direction']), $array); | |
print_r($array); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP数组不定长多键值排序 | |
* | |
* @author Zjmainstay | |
* @website http://www.zjmainstay.cn, https://glot.io/snippets/ernfkfnjhb | |
* @param array $list 数据源 | |
* @param array $rules 排序规则 ['key1'=>'asc', 'key2' => 'desc', ...] | |
* @return array | |
*/ | |
function smartMultiSort($list, $rules) { | |
$multisortParams = []; | |
foreach($rules as $key => $sort) { | |
$multisortParams[$key] = []; | |
$multisortParams[$key . $sort] = constant(strtoupper("sort_{$sort}")); | |
} | |
foreach($list as $item) { | |
foreach($rules as $key => $sort) { | |
$multisortParams[$key][] = $item[$key]; | |
} | |
} | |
$multisortParams[] = &$list; | |
call_user_func_array('array_multisort', $multisortParams); | |
return array_pop($multisortParams); | |
} | |
//示例 | |
$list = array (); | |
$list [] = array ( | |
'id' => 1, | |
'name' => '学生1', | |
'school' => '学校1', | |
'class' => '班级1' | |
); | |
$list [] = array ( | |
'id' => 4, | |
'name' => '学生4', | |
'school' => '学校2', | |
'class' => '班级2' | |
); | |
$list [] = array ( | |
'id' => 3, | |
'name' => '学生3', | |
'school' => '学校2', | |
'class' => '班级1' | |
); | |
$list [] = array ( | |
'id' => 2, | |
'name' => '学生2', | |
'school' => '学校1', | |
'class' => '班级2' | |
); | |
$list [] = array ( | |
'id' => 5, | |
'name' => '学生5', | |
'school' => '学校2', | |
'class' => '班级3' | |
); | |
print_r(smartMultiSort($list, ['school' => 'desc','id' => 'asc'])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 二维数组(例如:从数据读取一个数据列表。)按两个字段进行排序。使用冒泡排序。 | |
* | |
* @param array $arr 待排序数组。 | |
* @param array $ord 排序因子。 | |
* 例子:[ | |
* 0 => 'field1:asc', | |
* 1 => 'field2:desc', | |
* ]; | |
* | |
* 表示按field1升序排序之后,在field1的值相等的情况下,再按field2降序排序。 | |
* asc,desc是固定的值,没有其他可能值。 | |
* @return string | |
*/ | |
function twoFieldSorter($arr, $ord) | |
{ | |
if (empty($ord) or empty($arr)) { | |
return FALSE; | |
} | |
$len = count($arr); | |
$x = $arr; | |
list ($field1, $ord1) = explode(':', $ord[0]); | |
list ($field2, $ord2) = explode(':', $ord[1]); | |
// 第一个字段用usort()函数排序。 | |
if ($ord1 === 'asc') { | |
usort($x, function($a, $b)use($field1) { | |
$r = $a[$field1] - $b[$field1]; | |
return $r < 0 ? -1 : 1; | |
}); | |
} else { | |
usort($x, function($a, $b)use($field1) { | |
$r = $a[$field1] - $b[$field1]; | |
return $r > 0 ? -1 : 1; | |
}); | |
} | |
//二维排序 | |
for ($j = 0; $j < $len; $j++) { | |
for ($z = $j + 1; $z < $len; $z++) { | |
if ($x[$j][$field1] == $x[$z][$field1]) { | |
if ($ord2 === 'desc') { | |
if ($x[$j][$field2] < $x[$z][$field2]) { | |
$tmp = $x[$j]; | |
$x[$j] = $x[$z]; | |
$x[$z] = $tmp; | |
} | |
} else { | |
if ($x[$j][$field2] > $x[$z][$field2]) { | |
$tmp = $x[$j]; | |
$x[$j] = $x[$z]; | |
$x[$z] = $tmp; | |
} | |
} | |
} | |
} | |
} | |
return $x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment