Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
I believe this should serve your purpose,php.net/manual/en/array.sorting.php –  dreamweiver Oct 23 '13 at 6:47
add comment

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.

2 Answers

up vote 2 down vote accepted

use usort(), like:

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

usort($array, 'sortById');
echo "<pre>"; print_r($array);
share|improve this answer
    
quick and nice. –  Peter Oct 23 '13 at 6:58
add comment

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
*/
?> 
share|improve this answer
add comment

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