Symmetry Posted March 24, 2008 Share Posted March 24, 2008 I have an array that is for example structured as the following: $array['name'][index] $array['count'][index] or $array[index]['name'] $array[index]['count'] Where 'count' is a number of records associated with the 'name'.. I'd like to re-sort the array by 'count' descending, but I need to maintain an association to the 'name' values... array_multisort seems to reorganize the array by 'count' but I lose the 'name'. Thank you! Link to comment https://forums.phpfreaks.com/topic/97582-sorting-multidimensional-arrays/ Share on other sites More sharing options...
Symmetry Posted March 24, 2008 Author Share Posted March 24, 2008 I tried figuring out usort(), but no go. The array has multiple columns too, so it's not a simple case of 'name' and 'count'.. if that makes a difference.. thanks php'ers. ??? Link to comment https://forums.phpfreaks.com/topic/97582-sorting-multidimensional-arrays/#findComment-499272 Share on other sites More sharing options...
Barand Posted March 24, 2008 Share Posted March 24, 2008 try this <?php function mysort($a, $b) { return $b['count'] - $a['count']; } uasort($array, 'mysort'); ?> If it doesn't work, post the output from var_export($array); Link to comment https://forums.phpfreaks.com/topic/97582-sorting-multidimensional-arrays/#findComment-499290 Share on other sites More sharing options...
Symmetry Posted March 24, 2008 Author Share Posted March 24, 2008 I believe that worked Barand! Thank you, I guess I just wasn't grasping the function.. it was almost too simple Link to comment https://forums.phpfreaks.com/topic/97582-sorting-multidimensional-arrays/#findComment-499293 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.