Sashi Posted November 8, 2006 Share Posted November 8, 2006 I'm trying to sort out a multi-dimensional array, but I need to keep the associations intact and I'm not sure how to do this. I need to sort the multi-dimensional array by the values of the first array while keeping up the associations in the second array. Example: I need to take the following...[code]Array( [ID] => Array ( [0] => 2 [1] => 1 ) [NAME] => Array ( [0] => Fullmetal Alchemist [1] => Megaman NT Warrior ))[/code]...and have it sorted like this:[code]Array( [ID] => Array ( [0] => 1 [1] => 2 ) [NAME] => Array ( [0] => Megaman NT Warrior [1] => Fullmetal Alchemist ))[/code]If you need any other info, or if I just explained something badly here make sure to tell me! Oh yeah, and however I can do this needs to be able to sort the first array ascending. Thanks for your help ;D Link to comment https://forums.phpfreaks.com/topic/26575-solved-sorting-multi-dimensional-array-while-keeping-associations/ Share on other sites More sharing options...
Psycho Posted November 8, 2006 Share Posted November 8, 2006 Use array_multisort: http://us2.php.net/manual/en/function.array-multisort.phpIn your example, this would do it:array_multisort( $ar[ID], SORT_NUMERIC, SORT_ASC, $ar[NAME], SORT_STRING, SORT_ASC); Link to comment https://forums.phpfreaks.com/topic/26575-solved-sorting-multi-dimensional-array-while-keeping-associations/#findComment-121574 Share on other sites More sharing options...
Sashi Posted November 9, 2006 Author Share Posted November 9, 2006 Ah, I think I finally understand how array_multisort works now. Must've been too tired last night to really grasp it. Thanks for the help :D Link to comment https://forums.phpfreaks.com/topic/26575-solved-sorting-multi-dimensional-array-while-keeping-associations/#findComment-121932 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.