Maxoplata Posted July 22, 2010 Share Posted July 22, 2010 basically I have an array that looks something like this: $myarray = array(); $myarray[0] = array("5", "3", "6", "1", "3"); $myarray[1] = array("1", "8", "4", "2", "0"); $myarray[2] = array("9", "1", "0", "0", "5"); $myarray[3] = array("8", "7", "1", "5", "6"); $myarray[4] = array("0", "2", "4", "7", "9"); $myarray[5] = array("5", "5", "5", "7", "5"); I would like to sort the array so I can print out the data in a table, but I want to sort the array by the first value (0) in the inner array (so it would print out in the order 2,3,0,5,1,4). Can anyone help me figure this out? (In the long run I would like to order by 0,1,3 for when values are the same but just ordering by 0 will work for me for now). Link to comment https://forums.phpfreaks.com/topic/208550-ordering-an-array-via-an-element-with-an-array/ Share on other sites More sharing options...
bh Posted July 22, 2010 Share Posted July 22, 2010 Hi, with array_multisort() e.g. like this: $myarray = array(); $myarray[0] = array("5", "3", "6", "1", "3"); $myarray[1] = array("1", "8", "4", "2", "0"); $myarray[2] = array("9", "1", "0", "0", "5"); $myarray[3] = array("8", "7", "1", "5", "6"); $myarray[4] = array("0", "2", "4", "7", "9"); $myarray[5] = array("5", "5", "5", "7", "5"); $myarray_sort = array(); foreach ($myarray as $arr) { $myarray_sort[] = $arr[0]; } array_multisort($myarray, $myarray_sort); print_r($myarray); Link to comment https://forums.phpfreaks.com/topic/208550-ordering-an-array-via-an-element-with-an-array/#findComment-1089631 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.