anups Posted October 4, 2010 Share Posted October 4, 2010 How to sort PHP2d Array using upon key. eg. I have a array of $users[0]['name'] = "abc" $users[0]['age'] = "12" $users[0]['sex'] = "M"; $users[1]['name'] = "xyz" $users[1]['age'] = "15" $users[1]['sex'] = "M"; $users[2]['name'] = "pqr" $users[2]['age'] = "16" $users[2]['sex'] = "F"; Now I have to sort in on age or 'sex', Quote Link to comment https://forums.phpfreaks.com/topic/215112-array-sorting/ Share on other sites More sharing options...
Adam Posted October 4, 2010 Share Posted October 4, 2010 Use usort to define a function that will perform the sorting logic: function sortByAge($a, $b){ if ($a['age'] == $b['age']) { return 0; } return ($a['age'] < $b['age']) ? -1 : 1;}usort($users, 'sortByAge'); Quote Link to comment https://forums.phpfreaks.com/topic/215112-array-sorting/#findComment-1118844 Share on other sites More sharing options...
anups Posted October 4, 2010 Author Share Posted October 4, 2010 thanks solved Quote Link to comment https://forums.phpfreaks.com/topic/215112-array-sorting/#findComment-1118847 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.