Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting array according to the values in PHP

I have the following array

[0] => Array
    (
        [id] => 229
        [val] => 2
    )

[3] => Array
    (
        [id] => 237
        [val] => 1
    )

[4] => Array
    (
        [id] => 238
        [val] => 6
    )

I need to sort this array according to the val values in the array, and do not know how to accomplish this?

like image 700
Elitmiar Avatar asked Sep 02 '26 23:09

Elitmiar


1 Answers

function cmp($a, $b)
{
    if ($a["val"] == $b["val"]) {
        return 0;
    }
    return ($a["val"] < $b["val"]) ? -1 : 1;
}

usort($yourarray, "cmp");

Read this for more information.

like image 63
kjagiello Avatar answered Sep 05 '26 14:09

kjagiello