Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php array sort within array based on the value [duplicate]

Tags:

php

I have array like below

Array
(
    [0] => Array
        (
            [0] => 1280
            [id] => 1280
        )

    [1] => Array
        (
            [0] => 2261
            [id] => 2261
        )

    [2] => Array
        (
            [0] => 1280
            [id] => 1280
        )
)

In php, How do I sort from low to high based on the value of "id" ?

like image 887
Peter Avatar asked Oct 23 '13 06:10

Peter


1 Answers

use usort(), like:

function sortById($x, $y) {
    return $x['id'] - $y['id'];
}

usort($array, 'sortById');
echo "<pre>"; print_r($array);
like image 78
Sudhir Bastakoti Avatar answered Oct 19 '22 20:10

Sudhir Bastakoti