-1

I'm trying to arrange an array based on the value of the elements. Anyway here are my array code:

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
    )
)

is there any way for me to arrange them based on match and will look like this?

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
    )
)

Is there any php codeigniter code for this? feel free to check my code here https://eval.in/599473

Thanks!

howardtyler
  • 103
  • 2
  • 12

1 Answers1

4

You can achieve your goal using plain PHP code:

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

Reference: usort

Andrii Mishchenko
  • 2,626
  • 21
  • 19