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.

Possible Duplicate:
Sort multidimensional Array by Value (2)

array(200) {
  [0]=>
  array(5) {
    ["cat"]=>
    string(6) "Movies"
    ["name"]=>
    string(22) "Life.of.Pi.2012.DVDSCR"
    ["url"]=>
    string(62) "https://thepiratebay.se/torrent/8036528/Life.of.Pi.2012.DVDSCR"
    ["seed"]=>
    string(5) "33981"
    ["leech"]=>
    string(5) "18487"
  }
  [1]=>
  array(5) {
    ["cat"]=>
    string(6) "Movies"
    ["name"]=>
    string(41) "Django Unchained 2012 DVDSCR X264 AAC-P2P"
    ["url"]=>
    string(81) "https://thepiratebay.se/torrent/7990804/Django_Unchained_2012_DVDSCR_X264_AAC-P2P"
    ["seed"]=>
    string(5) "34279"
    ["leech"]=>
    string(5) "12256"
  }
...
}

I have such an array as shown above, and I would like to sort this array by seed indicator. How to achieve this in php?

share|improve this question

marked as duplicate by Michael Berkowski, Levi Morrison, Oldskool, Ed Heal, dgw Jan 21 '13 at 12:32

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.

    
...in particular, most easily done with usort() as described in a couple of answers there. –  Michael Berkowski Jan 21 '13 at 0:30

2 Answers 2

up vote 3 down vote accepted

You can use usort() like this.

usort($torrentList, function($a, $b) {
    return $a['seed'] - $b['seed'];
});

Where $torrentList is the the array you showed us above. Documentation can be found here.

share|improve this answer
    
Congratulations, you beat me to the answer by 14 seconds. +1 –  rink.attendant.6 Jan 21 '13 at 0:35
    
I'm surprised I wasn't the one who was beaten, happens to me more often than not. –  Austin Brunkhorst Jan 21 '13 at 0:37
    
Cheers, it's easier than I thought. –  ThiSGUy Jan 21 '13 at 0:44
usort($the_array, function($a, $b) {
    return ($a['seed'] - $b['seed']);
});
share|improve this answer

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