m1key Posted April 11, 2008 Share Posted April 11, 2008 Hi, I've got an array which currently looks like this: array(3) { [0]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(5) [4]=> int(9) } [1]=> array(5) { [0]=> int(2) [1]=> int(0) [2]=> int(6) [3]=> int(4) [4]=> int(16) } [2]=> array(5) { [0]=> int(3) [1]=> int(1) [2]=> int(5) [3]=> int(4) [4]=> int(11) } } How can I sort it so that it sorts on index value '4' descending - i.e, I want the array to look like this: array(3) { [0]=> array(5) { [0]=> int(2) [1]=> int(0) [2]=> int(6) [3]=> int(4) [4]=> int(16) <--------- Highest value } [1]=> array(5) { [0]=> int(3) [1]=> int(1) [2]=> int(5) [3]=> int(4) [4]=> int(11) } [2]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(5) [4]=> int(9) <--------- Lowest value } } Cheers, Mike. Link to comment https://forums.phpfreaks.com/topic/100653-solved-sorting-an-array-in-this-way/ Share on other sites More sharing options...
laffin Posted April 11, 2008 Share Posted April 11, 2008 array_sort($myarray[3]) Link to comment https://forums.phpfreaks.com/topic/100653-solved-sorting-an-array-in-this-way/#findComment-514765 Share on other sites More sharing options...
sasa Posted April 11, 2008 Share Posted April 11, 2008 look usort() function Link to comment https://forums.phpfreaks.com/topic/100653-solved-sorting-an-array-in-this-way/#findComment-514774 Share on other sites More sharing options...
m1key Posted April 11, 2008 Author Share Posted April 11, 2008 Hi, Many thanks for the response. I'm not sure if usort() will be entirely appropriate, because this array is essentially a collection of user-collected data, and could possibly contain 50+ sets. I only included 3 sets in my example above for comparison purposes. Apologies for not stating this in my original post Link to comment https://forums.phpfreaks.com/topic/100653-solved-sorting-an-array-in-this-way/#findComment-514805 Share on other sites More sharing options...
laffin Posted April 11, 2008 Share Posted April 11, 2008 so do u want to sort all arrays or only specific ones? if u are sorting all arrays, u will be looking at a recursive sort routine (which checks for any arrays within the main array) Link to comment https://forums.phpfreaks.com/topic/100653-solved-sorting-an-array-in-this-way/#findComment-514809 Share on other sites More sharing options...
m1key Posted April 11, 2008 Author Share Posted April 11, 2008 Ah, I think I was after usort() after all! This PHP lark is confusing sometimes This did what I was after: function highest($b, $a) { return $a[4] > $b[4]; } usort($finalResults,'highest'); Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/100653-solved-sorting-an-array-in-this-way/#findComment-514826 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.