嗨如何排序一個多維數組,取決於3個值,ps_kind,ps_date,ps_premium。 我會感謝任何答案或片段。由三個值排序多維數組
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++;
}
這不是一個網站,您可以免費索取代碼!向我們展示您嘗試過的方式,我們將幫助您。 – ComFreek
我沒有打算要求代碼bcz我寫了「或片段」,所以我認爲有人可以寫線索或somthing – wrcode
我想你可以使用[''usort'](http://php.net/manual /en/function.usort.php)與一個自定義函數。 – Sumurai8