nadeemshafi9 Posted October 8, 2007 Share Posted October 8, 2007 hello guys i need to sort a multidimentional array both numericaly and alphabeticaly on seperate occasions, the sort function dosent seem to work the array looks like this. $zone_array[1]["id"] = "1" $zone_array[1]["name"] = "ablah" $zone_array[1]["listitem"] = "<li>blah</li>" $zone_array[2]["id"] = "2" $zone_array[2]["name"] = "bblah" $zone_array[2]["listitem"] = "<li>blah</li>" $zone_array[3]["id"] = "3" $zone_array[3]["name"] = "cblah" $zone_array[3]["listitem"] = "<li>blah</li>" $zone_array[4]["id"] = "4" $zone_array[4]["name"] = "dblah" $zone_array[4]["listitem"] = "<li>blah</li>" the id's and names may be in different places as they are pulled out of the DB and may be in incorrect corelation with the numeric key. how can i sort them alphabeticaly, i know how to sort but i have trouble referencing this type of array. i need to sort it by the id and then on another ocasion by the title. Quote Link to comment https://forums.phpfreaks.com/topic/72294-solved-sort-multi-dimentional-array/ Share on other sites More sharing options...
nadeemshafi9 Posted October 8, 2007 Author Share Posted October 8, 2007 i done it Quote Link to comment https://forums.phpfreaks.com/topic/72294-solved-sort-multi-dimentional-array/#findComment-364554 Share on other sites More sharing options...
MasterACE14 Posted October 8, 2007 Share Posted October 8, 2007 click "Topic Solved" Quote Link to comment https://forums.phpfreaks.com/topic/72294-solved-sort-multi-dimentional-array/#findComment-364583 Share on other sites More sharing options...
nadeemshafi9 Posted October 9, 2007 Author Share Posted October 9, 2007 function msort($array, $id) { $temp_array = array(); while(count($array)>0) { $lowest_id = 0; $index=0; foreach ($array as $item) { if (isset($item[$id]) && $array[$lowest_id][$id]) { if ($item[$id]<$array[$lowest_id][$id]) { $lowest_id = $index; } } $index++; } $temp_array[] = $array[$lowest_id]; $array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1)); } return $temp_array; } Quote Link to comment https://forums.phpfreaks.com/topic/72294-solved-sort-multi-dimentional-array/#findComment-365271 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.