如何使用php codeigniter以最快的方式基于值对数组进行排序?

时间:2016-07-02 10:11:30

标签: php arrays codeigniter sorting multidimensional-array

我试图根据元素的值来安排数组。无论如何这里是我的数组代码:

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

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用纯PHP代码实现目标:

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

参考:usort