This question already has an answer here:

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" ?

marked as duplicate by deceze, Pragnesh Chauhan, James Donnelly, rds, Jim Oct 23 '13 at 11:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 11 down vote accepted

use usort(), like:

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

usort($array, 'sortById');
echo "<pre>"; print_r($array);
  • quick and nice. – Peter Oct 23 '13 at 6:58
  • You are rocking man, thanks for quick – Suresh Suthar Sep 23 '17 at 14:51

use array_multisort() like below:

<?php
$multiArray = Array(
    Array("id" => 120, "name" => "Defg"),
    Array("id" => 62, "name" => "Abcd"),
    Array("id" => 99, "name" => "Bcde"),
    Array("id" => 2, "name" => "Cdef"));
$tmp = Array();
foreach($multiArray as &$ma)
    $tmp[] = &$ma["id"];
array_multisort($tmp, $multiArray);
foreach($multiArray as &$ma)
    echo $ma["id"]."<br/>";

/* Outputs
    2
62
99
120
*/
?> 

Not the answer you're looking for? Browse other questions tagged or ask your own question.