bdee1 Posted January 26, 2009 Share Posted January 26, 2009 i have a 2 dimensional array which is basically an array where each element contains an associative array. one of the keys of the associative array is id which contains a numeric value. how woudl i go about sorting the root array in the order of the id element from the associative arrays? Quote Link to comment https://forums.phpfreaks.com/topic/142538-sortign-a-two-dimensional-array/ Share on other sites More sharing options...
MadTechie Posted January 26, 2009 Share Posted January 26, 2009 to Sort an array and maintain index association use asort() EDIT: if you want to sort by first item try this <?php aksort($array); print_r($array); function aksort(&$array) { asort($array); $vals = array_count_values($array); $i = 0; foreach ($vals AS $val=>$num) { $first = array_splice($array,0,$i); $tmp = array_splice($array,0,$num); ksort($tmp); $array = array_merge($first,$tmp,$array); unset($tmp); $i = $i+$num; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142538-sortign-a-two-dimensional-array/#findComment-746958 Share on other sites More sharing options...
bdee1 Posted January 28, 2009 Author Share Posted January 28, 2009 hey thanks for the reply but i am not sure i am able to follow your code snippet any chance you could explain it a bit further? Quote Link to comment https://forums.phpfreaks.com/topic/142538-sortign-a-two-dimensional-array/#findComment-748631 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.