cksort
Pin Young Lv9

对数组某一键值进行排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 对数组某一键值排序
* @author Feei
* @param $array
* @param string $subkey
* @param null $subkey2
* @param bool $sort_ascending
*/
public static function cksort(&$array, $subkey = "id", $subkey2 = null ,$sort_ascending=false)
{
if (count($array))
$temp_array[key($array)] = array_shift($array);

foreach($array as $key => $val){
$offset = 0;
$found = false;
foreach($temp_array as $tmp_key => $tmp_val)
{
if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
{
$temp_array = array_merge(
(array)array_slice($temp_array,0,$offset), array($key => $val),
array_slice($temp_array,$offset));
$found = true;
}
elseif(!$found
and $subkey2 and strtolower($val[$subkey]) == strtolower($tmp_val[$subkey])
and strtolower($val[$subkey2]) > strtolower($tmp_val[$subkey2]))
{
$temp_array = array_merge(
(array)array_slice($temp_array,0,$offset),
array($key => $val), array_slice($temp_array,$offset));
$found = true;
}
$offset++;
}
if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
}
if ($sort_ascending) $array = array_reverse($temp_array);
else $array = $temp_array;
}