嗨,我如何根据 ps_kind,ps_date,ps_premium 的 3 个值对多维数组进行排序。我会感谢任何答案或片段。
Array
(
[0] => Array
(
[ps_date] => 2013-08-05 20:56:33
[ps_kind] => Gold
[ps_premium] = > 1
)
[1] => Array
(
[ps_date] => 2013-08-05 20:46:33
[ps_kind] => Gold
[ps_premium] = > 0
)
[2] => Array
(
[ps_date] => 2013-08-05 20:16:33
[ps_kind] => Silver
[ps_premium] = > 0
)
[3] => Array
(
[ps_date] => 2013-08-05 20:06:33
[ps_kind] => Bronze
[ps_premium] = > 0
)
)
我一直在尝试使用此代码,但他无法正常工作
function cmp($a, $b)
{
$pos = array (
'Gold' => 1,
'Silver' => 2,
'Bronze' => 3,
);
list ($a1, $c1) = explode('', $a['ps_kind']);
list ($a2, $c2) = explode('', $b['ps_kind']);
$catcmp = strcmp(trim($c1), trim($c2));
if ($catcmp==0)
return $pos[trim($a1)] - $pos[trim($a2)];
else return $catcmp;
}
$result = $this -> db ->query($query);
foreach ( $result as $element ) {
$array[] = $element;
}
usort($array,'cmp');
$index = 0;
foreach ($array as $single ) {
$count = $index + 1;
if($array[$index]['ps_date'] < $array[$count]['ps_date'] && $array[$index]['ps_kind'] == $array[$count]['ps_kind'] ) {
$prev = $array[$index];
$next = $array[$count];
$array[$index] = $next;
$array[$count] = $prev;
}
$index++;
}