2016-07-02 39 views
-1

我想安排一個基於元素值的數組。反正這裏是我的陣列碼:如何以最快的方式使用php codeigniter對數組進行排序?

array (
    [0] => array (
     [id] => 5 
     [title] => yesyes3 
     [msg] => yes yes yes yes 
     [date] => 2016-06-05 
     [match] => 1 
    ) 
    [1] => array (
     [id] => 4 
     [title] => yes2 
     [msg] => yes yes yes 
     [date] => 2016-06-04 
     [match] => 4 
    ) 
    [2] => array (
     [id] => 1 
     [title] => test1 
     [msg] => yes 
     [date] => 2016-06-01 
     [match] => 2 
    ) 
    [3] => array (
     [id] => 3 
     [title] => test2 
     [msg] => no 
     [date] => 2016-06-03 
     [match] => 1 
    ) 
    [4] => array (
     [id] => 2 
     [title] => yes1 
     [msg] => no 
     [date] => 2016-06-02 
     [match] => 6 
    ) 
) 

有沒有什麼辦法,我根據比賽安排他們看起來像這樣?

array (
    [0] => array (
     [id] => 2 
     [title] => yes1 
     [msg] => no 
     [date] => 2016-06-02 
     [match] => 6 
    ) 
    [1] => array (
     [id] => 4 
     [title] => yes2 
     [msg] => yes yes yes 
     [date] => 2016-06-04 
     [match] => 4 
    ) 
    [2] => array (
     [id] => 1 
     [title] => test1 
     [msg] => yes 
     [date] => 2016-06-01 
     [match] => 2 
    ) 
    [3] => array (
     [id] => 3 
     [title] => test2 
     [msg] => no 
     [date] => 2016-06-03 
     [match] => 1 
    ) 
    [4] => array (
     [id] => 5 
     [title] => yesyes3 
     [msg] => yes yes yes yes 
     [date] => 2016-06-05 
     [match] => 1 
    ) 
) 

是否有任何php codeigniter代碼?請隨時查看我的代碼https://eval.in/599473

謝謝!

+2

使用usort()通過'match' – splash58

回答

2

您可以使用普通的PHP代碼實現自己的目標:

usort($array, function ($one, $two) { 
    if ($one['match'] === $two['match']) { 
     return 0; 
    } 
    return $one['match'] > $two['match'] ? -1 : 1; 
}); 

參考:usort

+1

訂購陣列只是'返回$酮[ '匹配'] - $ two ['match'];'將工作 – splash58

+0

真棒,但這樣做會顯示升序@ splash58 – howardtyler

+0

這意味着'返回$ 2 ['match'] - $ 1 ['match'];':) – splash58

相關問題