rupam_jaiswal Posted November 17, 2013 Share Posted November 17, 2013 Hi, Please excuse if similar issue has been posted earlier, my array is like this newScore1 is Array( [0] => Array ( [scoreid] => 12 [compid] => 4 [name] => andrew [score1] => 4 [score2] => 122 [score3] => 12 [score4] => DNF [rank1] => 5 ) [1] => Array ( [scoreid] => 15 [compid] => 4 [name] => Ed [score1] => DNF [score2] => DNF [score3] => 34 [score4] => DNF [rank1] => 0 ) [2] => Array ( [scoreid] => 14 [compid] => 4 [name] => andy [score1] => 34 [score2] => 21 [score3] => 34 [score4] => 34 [rank1] => 4 ) [3] => Array ( [scoreid] => 13 [compid] => 4 [name] => crusty [score1] => 1 [score2] => 0 [score3] => 34 [score4] => 0 [rank1] => T2 ) [4] => Array ( [scoreid] => 10 [compid] => 4 [name] => larry g [score1] => 1 [score2] => 1 [score3] => 23 [score4] => 34 [rank1] => T2 ) [5] => Array ( [scoreid] => 11 [compid] => 4 [name] => alex [score1] => 0 [score2] => 33 [score3] => 0 [score4] => 44 [rank1] => 0 )) I have to sort array with score1 values in ascending or descending order but if the value is 0 and DNF then it should always be at the bottom. eg priority_array = (0,'DNF')1) ascending ordernewScore1 is Array( [3] => Array ( [scoreid] => 13 [compid] => 4 [name] => crusty [score1] => 1 [score2] => 0 [score3] => 34 [score4] => 0 [rank1] => T2 ) [4] => Array ( [scoreid] => 10 [compid] => 4 [name] => larry g [score1] => 1 [score2] => 1 [score3] => 23 [score4] => 34 [rank1] => T2 ) [0] => Array ( [scoreid] => 12 [compid] => 4 [name] => andrew [score1] => 4 [score2] => 122 [score3] => 12 [score4] => DNF [rank1] => 5 ) [2] => Array ( [scoreid] => 14 [compid] => 4 [name] => andy [score1] => 34 [score2] => 21 [score3] => 34 [score4] => 34 [rank1] => 4 ) [1] => Array ( [scoreid] => 15 [compid] => 4 [name] => Ed [score1] => DNF [score2] => DNF [score3] => 34 [score4] => DNF [rank1] => 0 ) [5] => Array ( [scoreid] => 11 [compid] => 4 [name] => alex [score1] => 0 [score2] => 33 [score3] => 0 [score4] => 44 [rank1] => 0 ))2) descending ordernewScore1 is Array( [2] => Array ( [scoreid] => 14 [compid] => 4 [name] => andy [score1] => 34 [score2] => 21 [score3] => 34 [score4] => 34 [rank1] => 4 ) [0] => Array ( [scoreid] => 12 [compid] => 4 [name] => andrew [score1] => 4 [score2] => 122 [score3] => 12 [score4] => DNF [rank1] => 5 ) [3] => Array ( [scoreid] => 13 [compid] => 4 [name] => crusty [score1] => 1 [score2] => 0 [score3] => 34 [score4] => 0 [rank1] => T2 ) [4] => Array ( [scoreid] => 10 [compid] => 4 [name] => larry g [score1] => 1 [score2] => 1 [score3] => 23 [score4] => 34 [rank1] => T2 ) [1] => Array ( [scoreid] => 15 [compid] => 4 [name] => Ed [score1] => DNF [score2] => DNF [score3] => 34 [score4] => DNF [rank1] => 0 ) [5] => Array ( [scoreid] => 11 [compid] => 4 [name] => alex [score1] => 0 [score2] => 33 [score3] => 0 [score4] => 44 [rank1] => 0 )) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/283990-sort-mutlidimensional-array-with-certain-values-always-at-the-last/ Share on other sites More sharing options...
Solution Barand Posted November 17, 2013 Solution Share Posted November 17, 2013 You just need a couple of custom sort functions, one for ascending and the other for descending sorts usort($newScore1, 'mysortAsc'); // or // usort($newScore1, 'mysortDesc'); function mysortAsc($a, $b ) { if (in_array($a['score1'], array(0,'DNF'))) return 1; if (in_array($b['score1'], array(0,'DNF'))) return -1; return $a['score1'] - $b['score1']; } function mysortDesc($a, $b ) { if (in_array($a['score1'], array(0,'DNF'))) return 1; if (in_array($b['score1'], array(0,'DNF'))) return -1; return $b['score1'] - $a['score1']; } Quote Link to comment https://forums.phpfreaks.com/topic/283990-sort-mutlidimensional-array-with-certain-values-always-at-the-last/#findComment-1458656 Share on other sites More sharing options...
rupam_jaiswal Posted November 18, 2013 Author Share Posted November 18, 2013 Thanks for your reply. Regards Quote Link to comment https://forums.phpfreaks.com/topic/283990-sort-mutlidimensional-array-with-certain-values-always-at-the-last/#findComment-1458809 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.