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', 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'); 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 Link to comment https://forums.phpfreaks.com/topic/215112-array-sorting/#findComment-1118847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.