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.

I have a multi-dimensional array like so.

[0] => Array
    (
        [time] => 1312206805
        [item] => [email protected] created ad ad.
    )

[1] => Array
    (
        [time] => 1312206805
        [item] => [email protected] created an ad.
    )

[2] => Array
    (
        [time] => 1312130982
        [item] => [email protected] created an ad.
    )

I'd like to sort by the time, while maintaining indexes and keeping each parent array together.

share|improve this question
3  
Please check your older questions and accepted correct answers. Community helps you. Why you don't help community ? –  RiaD Aug 1 '11 at 20:09
    
possible duplicate of Sorting a multidimensional array in PHP? ... or one of other dozens like it on SO... just use uasort. –  Wrikken Aug 1 '11 at 20:10

1 Answer 1

up vote 3 down vote accepted
uasort($arr,function($a,$b){
    return $a['time'] - $b['time'];
});

Requires php5.3+
If you use older version you can replace anonymous function by other callback

share|improve this answer
    
@stereofrog, thank you, fixed. mixed up it with C++ –  RiaD Aug 1 '11 at 20:21
    
Thanks! This worked great. Just what I needed. Saved me a few hours of guess and check. :) –  ndmweb Aug 1 '11 at 22:01
    
it is great chance to use advice in my comment to your question –  RiaD Aug 2 '11 at 10:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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