jingo_man Posted May 29, 2007 Share Posted May 29, 2007 hi, i have read the documentation around the array_multisort() function. this however seems to sort horizontally, where as i need to do so vertically. does anyone know how to get around this? for example, array_multisort() will work with this array: (0,0) = A (0,1) = B (0,2) = C (0,3) = D (1,0) = E (1,1) = F (1,2) = G (1,3) = H so it structurally looks like: A B C D E F G H my array, however is: Hole1, Score1 Hole2, Score2 and i need to sort the "Score" column... many thanks for any help. jingo_man Quote Link to comment https://forums.phpfreaks.com/topic/53424-sorting-multidimensional-array/ Share on other sites More sharing options...
Wildbug Posted May 29, 2007 Share Posted May 29, 2007 Untested, but something like so... usort($array,create_function('$a,$b','return ($a[1] == $b[1] ? 0 : ($a[1] < $b[1] ? -1 : 1))')); ...will make it possible to sort by a given column -- in this case the second, [1]. Quote Link to comment https://forums.phpfreaks.com/topic/53424-sorting-multidimensional-array/#findComment-264009 Share on other sites More sharing options...
jingo_man Posted May 29, 2007 Author Share Posted May 29, 2007 thanks for the heads up, wildbug. using your code as a base, and searching around it, i amended it slightly to work with a more advanced array. i am now intending to create the compare() function to accept an extra parameter so i can check other values. the function is: function compare($a, $b) { if ($a == $b) { return 0; } return ($a[25] < $b[25]) ? -1 : 1; } the array sorting is called with "ucase($array,"compare"); ----- i would like to change it so the number 25 (column no of the array) can be changed. thought i could add a 3rd parameter to compare() and specify which column i am after, but i cant interpret how $a and $b get their values - "compare" isnt a typical function call with parameters... thanks again... jingo_man Quote Link to comment https://forums.phpfreaks.com/topic/53424-sorting-multidimensional-array/#findComment-264175 Share on other sites More sharing options...
Barand Posted May 29, 2007 Share Posted May 29, 2007 <?php function compare($a, $b) { global $sortcol; if ($a[$sortcol] == $b[$sortcol]) { return 0; } return ($a[$sortcol] < $b[$sortcol]) ? -1 : 1; } // the array sorting is called with $sortcol = 25; usort($array,"compare"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53424-sorting-multidimensional-array/#findComment-264196 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.