Telmo
Telmo

Reputation: 21

Sort rows of a 2d array by a column of values in descending order

I am adding elements to multidimensional array like this

foreach($results as $res){
    $fwidth = $res[0];
    $fpath = $res[1];

    $sub = array(
        'img_w' => $fwidth,
        'img_path' => $fpath,
    );

    $widths[] = $sub;
}

And I want to sort $widths array by 'img_w' from bigger to smaller (DESC).

Upvotes: 1

Views: 99

Answers (1)

Jan Hančič
Jan Hančič

Reputation: 53931

Use PHP's USort() function (user sort), which takes two arguments. The first is the array you want to sort. The second is a function that does the actual sorting (you write it yourself). The page I linked to has some examples that should cover your case.

Upvotes: 1

Related Questions