ninedoors Posted July 15, 2009 Share Posted July 15, 2009 I know that there has to be a function out there that sorts multi-dimensional array other than array_multisort. I pull this data out of a database into an array with the structure: $array[event] = count; where event is an integer for a given time period like the winter season and member is just the member number of one of my members. So I just increment $array[event] everytime is is pulled out and then I have an array with a bunch of counts. The thing is I don't have how many different events, of members there will be as the user is selecting the data by way of a form. What I need to be able to sort this array by the count value so I would start with something like this: $array[1][120] = 3; $array[2][120] = 2; $array[2][151] = 1; $array[1][153] = 0; $array[3][162] = 10; $array[2][12] = 2; $array[2][1] = 111; $array[1][34] = 3; And would liekto get this back: $array[2][1] = 111; $array[3][162] = 10; $array[2][120] = 3; $array[2][34] = 3; $array[1][120] = 2; $array[2][12] = 2; $array[2][151] = 1; $array[1][153] = 0; Maybe I am missing something but I can't get my head around it right now. Thanks for the help. Nick Link to comment https://forums.phpfreaks.com/topic/166087-solved-sorting-a-multi-dimensional-array/ Share on other sites More sharing options...
rhodesa Posted July 15, 2009 Share Posted July 15, 2009 You can't sort like that on arrays. You could do this instead though: $array["$event_$member"] = $count; Which would give you: $array[1][120] = 3; $array[2_120] = 2; $array[2_151] = 1; $array[1_153] = 0; $array[3_162] = 10; $array[2_12] = 2; $array[2_1] = 111; $array[1_34] = 3; then sort it descending keeping the indexes: arsort($array); then loop over it: foreach($array as $k=>$count){ list($event,$member) = explode('_',$k,2); } Link to comment https://forums.phpfreaks.com/topic/166087-solved-sorting-a-multi-dimensional-array/#findComment-875897 Share on other sites More sharing options...
ninedoors Posted July 15, 2009 Author Share Posted July 15, 2009 Thanks I will give that a try. Nick Link to comment https://forums.phpfreaks.com/topic/166087-solved-sorting-a-multi-dimensional-array/#findComment-875903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.